SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    case when problem

    I do a join from 2 tables and I want when a field has a value to take one field and if not to take another field.
    Something like this.
    Code:
    select user_id, 
    case when user_id=1 then field1
    else
    field2
    end
    from table1, table2
    How can I do that?

  2. #2
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to clarify; do you mean that if the column in table 1 has no value that you then want to get the value from the col in table 2?

    bazz

  3. #3
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My problem is that I want the field1 value if that belongs to user_id (some table) else the field2 of the other table.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by dimis283 View Post
    How can I do that?
    just like that

    try it and let us know if you have any problems


    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
  •