Log in

View Full Version : mySQL - if...else condition help....



cancer10
08-18-2008, 04:35 PM
Hi there,


I have a table - mytable and the columns in the table are as follows:


CityName (varchar)
updated_by (varchar)
added_by (varchar)

The first column is a city column, someone would add/update the city names and accordingly their name would be updated/added to the updated_by and added_by columns. Now, the condition of my query would be:

- if the updated_by column is NOT empty and the added_by column is empty, then the value of the updated_by would show .

- if the added_by column is NOT empty and the updated_by column is empty, then the value of the added_by column would show.

- if both added_by and updated_by columns are NOT empty then the value of the updated_by would show.



Could this be done in one single query? If yes, how (example plz)?


Thanx in advance

Twey
08-18-2008, 05:36 PM
SELECT CityName, IFNULL(updated_by, added_by, updated_by) AS latest FROM mytable;You get NULL if they're both empty. I wasn't sure what you meant by 'show' (this is MySQL after all) so I assigned the result to a column called 'latest'.

cancer10
08-19-2008, 03:06 AM
By "show" I meant "display"

Twey
08-19-2008, 03:16 AM
Uhm... MySQL is not a general-purpose programming language: it is not capable of explicit IO. It can only store and retrieve data.

cancer10
08-19-2008, 03:21 AM
but it does return results, thats was wot i actually meant :)

sorry for the confusion