If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > mySQL - if...else condition help....

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-18-08, 12:35
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
Question mySQL - if...else condition help....

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
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10
Reply With Quote
  #2 (permalink)  
Old 08-18-08, 12:39
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
either a stored procedure (assuming you are using MySQL 5 or above)

or in program logic
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 08-18-08, 12:42
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
not possible in mysql query itself?
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10
Reply With Quote
  #4 (permalink)  
Old 08-18-08, 13:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
healdem, are you kidding? it's dead simple
Code:
SELECT COALESCE(updated_by,added_by) as mydate
  FROM ...
cancer, this relies on "empty" being NULL, and not the empty string

and you should, of course, be using DATE datatypes, not VARCHAR
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-18-08, 15:43
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
Quote:
Originally Posted by r937
healdem, are you kidding? it's dead simple
Code:
SELECT COALESCE(updated_by,added_by) as mydate
  FROM ...
cancer, this relies on "empty" being NULL, and not the empty string

and you should, of course, be using DATE datatypes, not VARCHAR

oops
misread it as a how do I update......
mea culpa
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On