SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    737
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    697
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select cityname,
             coalesce(updated_by,added_by)
       from t

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,248
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    ... and COALESCE depends, of course, on "empty" actually being NULL, and not the empty string

    and the dates should be DATE datatype, not VARCHAR

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    737
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    ... and COALESCE depends, of course, on "empty" actually being NULL, and not the empty string

    and the dates should be DATE datatype, not VARCHAR

    well, there is NO date field. Where did u get one?

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,248
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    oh, my bad, sorry -- the columns are updated_by and added_by, not updated_date and added_date

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •