开发者

MySQL join not returning all results

开发者 https://www.devze.com 2023-02-17 02:57 出处:网络
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

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

精彩评论

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