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

Discussion in 'MySQL' started by cancer10, Aug 18, 2008.

  1. #1
    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
     
    cancer10, Aug 18, 2008 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    More effective way to do this is to use application logic.
    Try query below to get desired behavior.

    Select last_touch from
    (
    SELECT updated_by as last_touch FROM mytable where updated_by is not null and added_by is not null
    UNION
    SELECT updated_by as last_touch FROM mytable where updated_by is not null and added_by is null
    UNION
    SELECT added_by as last_touch FROM mytable where updated_by is null and added_by is not null
    ) ifelse
    limit 1
     
    cont911, Aug 19, 2008 IP