sbayeta Posted June 3, 2003 Share Posted June 3, 2003 Hi, I have a table with the fields \"id\", \"value\", \"value_on_nick\" and \"value_of_nick\", and some rows that look like: | id | value | value_on_nick | value_off_nick | |-------|-------|---------------|----------------| | item1 | 0 | abnormal | normal | | item2 | 1 | cold | hot | Is it possible to create a query that will return the item id and the nick corresponding to the value ? Like | id | nick | |-------|--------| | item1 | normal | | item2 | cold | Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/541-sql-question-is-it-possible-to/ Share on other sites More sharing options...
rhysmeister Posted June 4, 2003 Share Posted June 4, 2003 No sure if you mean something more complex, but if I understand... SELECT id, nick from <tablename>; Quote Link to comment https://forums.phpfreaks.com/topic/541-sql-question-is-it-possible-to/#findComment-1824 Share on other sites More sharing options...
pallevillesen Posted June 4, 2003 Share Posted June 4, 2003 Yes... use a select in combination with IF.... check the syntax of the IF, then write if you need more help... I.e. select ID, if (value =1, NICK_ON, nick_off) FROM TABLE.... The above is probably not the correct syntax, but you get the idea. P. Quote Link to comment https://forums.phpfreaks.com/topic/541-sql-question-is-it-possible-to/#findComment-1825 Share on other sites More sharing options...
sbayeta Posted June 4, 2003 Author Share Posted June 4, 2003 Thanks biopv. I found that yesterday, and it works fine. But doing some further researching, I discovered that there are ANSI SQL ways to do this: SELECT CASE value WHEN 0 THEN offnick ELSE onnick END AS val; Quote Link to comment https://forums.phpfreaks.com/topic/541-sql-question-is-it-possible-to/#findComment-1830 Share on other sites More sharing options...
pallevillesen Posted June 5, 2003 Share Posted June 5, 2003 Lovely... I didn\'t know that... Have fun, P. Quote Link to comment https://forums.phpfreaks.com/topic/541-sql-question-is-it-possible-to/#findComment-1841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.