Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2008
    Posts
    36

    Question Unanswered: 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
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    either a stored procedure (assuming you are using MySQL 5 or above)

    or in program logic
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Mar 2008
    Posts
    36
    not possible in mysql query itself?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    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 on the Tiger 800 or the Norton

Posting Permissions

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