Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,648 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,120 people online right now. Registration is fast and FREE... Join Now!




mySQL - if...else condition help....

 
Reply to this topicStart new topic

mySQL - if...else condition help....

cancer10
post 18 Aug, 2008 - 08:39 AM
Post #1


New D.I.C Head

*
Joined: 12 Jul, 2008
Posts: 5

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




User is offlineProfile CardPM

Go to the top of the page

mocker
post 18 Aug, 2008 - 09:09 AM
Post #2


D.I.C Regular

Group Icon
Joined: 14 Oct, 2007
Posts: 256



Thanked 15 times
My Contributions


mysql's if statement goes like

SELECT IF( (condition), (true), (false) ) fieldname

It looks kind of weird but its basically an if..else in one statement, or if you have used the (condition) ? true value : false value ; statement, it is like that.

I am going to assume you mean null when you say empty, in which case you'd want something like

SELECT IF( isnull(updated_by), added_by, updated_by) as person FROM table_name

If they are both null, then the value returned will be null anyways, otherwise it matches your conditions
User is offlineProfile CardPM

Go to the top of the page

AdaHacker
post 18 Aug, 2008 - 04:13 PM
Post #3


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 166



Thanked 26 times
My Contributions


If by "empty" you mean NULL, then a simpler and more ANSI-compatible way to do it would be the COALESCE function. It takes a list of arguments and returns the first one that isn't null.
SQL
SELECT COALESCE(updated_by, added_by) FROM yourtable;


If by "empty" you mean something else, then ANSI SQL defines (and MySQL implements) the CASE statement. You can use it for if-else style comparisons, such as:
SQL
SELECT CASE
WHEN updated_by = '' THEN added_by
WHEN added_by = '' THEN updated_by
ELSE updated_by
END FROM yourtable;
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:00AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month