We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24719
    • 194 Posts
    hi all,

    i’ve got a tricky sql statement to write. is it possible to write a query that performs a join on a column, but will also return rows which don’t join?

    the sql for the join is below:

    SELECT code,promo_price,c.id,m.months FROM promo_codes AS c 
    INNER JOIN num_months AS m ON m.`id`=c.`did` 


    i’m using promo_codes.did as a control column: if it equals 0, then it has a special meaning (num_months = ’all’ in that case). I don’t want to create a new entry in the num_months table for this value though.

    i think this could be done with sub-queries perhaps. what i want to say is really:

    SELECT code,promo_price,c.id,m.months 
    FROM promo_codes AS c 
    INNER JOIN num_months AS m ON m.`id`=c.`did` 
    OR SELECT * FROM promo_codes WHERE `did` = 0


    but this is not valid sql.

    has anyone any ideas?

    thanks
      • 15987
      • 786 Posts
      I think this should work for you.

      SELECT code,promo_price,c.id,m.months FROM promo_codes AS c 
      LEFT JOIN num_months AS m ON m.`id`=c.`did`
      
        • 24719
        • 194 Posts
        like a charm. thanks smiley