Results 1 to 5 of 5

Thread: mySQL - if...else condition help....

  1. #1
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    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
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    SELECT CityName, IFNULL(updated_by, added_by, updated_by) AS latest FROM mytable;
    You get NULL if they're both empty. I wasn't sure what you meant by 'show' (this is MySQL after all) so I assigned the result to a column called 'latest'.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. The Following User Says Thank You to Twey For This Useful Post:

    cancer10 (08-19-2008)

  4. #3
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    By "show" I meant "display"

  5. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Uhm... MySQL is not a general-purpose programming language: it is not capable of explicit IO. It can only store and retrieve data.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. The Following User Says Thank You to Twey For This Useful Post:

    cancer10 (08-19-2008)

  7. #5
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    but it does return results, thats was wot i actually meant

    sorry for the confusion

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
  •