开发者

MySQL join not returning all results

SELECT * FROM products AS p JOIN images AS i ON (p.id = i.product_id)

I want this query also to return results where t开发者_开发技巧here are no fields in "images" related to "products". How do I do that?


You need an OUTER join.

SELECT *
FROM   products AS p
       LEFT JOIN images AS i
         ON ( p.id = i.product_id )  

LEFT because you want to preserve all rows from the left (first) table.


You will need to use LEFT JOIN:

   SELECT * FROM 
      products AS p LEFT JOIN images AS i ON p.id = i.product_id;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜