SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mysql IF statement THEN JOIN

    Hey i'm writing a script at the moment and i need to write a query which will work like this

    SELECT * FROM `table`
    IF `table`.`field_1` = 'somthing'
    LEFT JOIN `another_table`
    ON `table`.`field_2` = `another_table`.`field_1`
    ELSE IF `table`.`field_1` = 'somthing_else'
    LEFT JOIN `another_table_2`
    ON `table`.`field_2` = `another_table_2`.`field_1`
    END IF
    WHERE `table`.`field_3` = 'keyword'

    I hope that makes sense. I've never used IF statements in mysql before and i can't find much documentation that helps.

    Thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't think you can do that. What you CAN do is outer join all the tables you might need, and use conditionals in the column list to do whatever you were trying to do here.

  3. #3
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working now, Cheers

  4. #4
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Martian,

    Can you post your query which works coz I have the same problem and did try it with outer join...but it isnt working...

    cheers


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
  •