Question

I have an MySQL database and a table table1 with columns:

  • id of uint type
  • some other, not important now fields (like name, age etc.),
  • status of enum type (met, like, love, hate, kill).

For the inserted row (1, love), i want to select the "next" status (hate).

My attempt:

I tried:

SELECT (status + 1) FROM table1 WHERE id = 1

but it retured 4 (index, not text value).

Of course:

SELECT status FROM table1 WHERE id = 1

returns love (text value, not index).

Question:

Is there an way to achive it (some kind of "casting" maybe)?

I preffer solution that do not mess with database schema tables.

Also, I could UPDATE that row with status = status + 1, than select it and after it UPDATE again with status = status - 1, but it's even worse idea...

Edit

As I have been asked to give some more details: I do not have any additional table that hold the order of my enum. I just added it with phpMyAdmin, and the selection from it works as I described. Moreover when I update the table with status = status + 1 i get hate from love.

Was it helpful?

Solution 2

Something like this?

SELECT * FROM test 
WHERE status+0=(SELECT (status + 0)+1 FROM test   WHERE id = 1)

SQL Fiddle

Here is the next status

SELECT (status + 0)+1 FROM test WHERE id = 1

OTHER TIPS

That is no that easy as you might think. How do you manage the order of your enum? Via ID? Via an ordering column? At least you have to work with sub-queries or something like that. Could you give us some more detailed information?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top