Jump to content

Archived

This topic is now archived and is closed to further replies.

sbayeta

SQL question. Is it possible to....

Recommended Posts

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!

Share this post


Link to post
Share on other sites

No sure if you mean something more complex, but if I understand...

 

SELECT id, nick from <tablename>;

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.