![](/img/trans.png)
[英]codeigniter join 3 tables and get data from 1st table where 1st table is linked with 2nd table and 2nd table is linked with 3rd table
[英]LEFT OUTER JOIN with WHERE clause on 2nd table, not effecting 1st table
我正在尝试从两个表返回一组记录。 我正在使用LEFT OUTER JOIN
,因为我想要表1中的所有数据。如果满足某个子句,我只希望表2中的数据为JOINED
。
如果表2中的记录不符合WHERE
子句,则该子句似乎将覆盖LEFT OUTER JOIN
并且不返回表1的记录。
SELECT p.code, p.name, d.product_code, d.detail_type, d.description
FROM products p
LEFT OUTER JOIN product_details d
ON p.code = d.product_code
WHERE product_details.detail_type = 'ALTERNATIVE NAMES'
我想从产品的所有行返回,但我只想从行product_details
到,如果要加入product_details.detail_type = 'ALTERNATIVE NAMES'
我正在寻找有关是否可行的指导,或者是否应该在初始JOIN
之后去除不需要的数据?
在ON Clause
使用product_details.detail_type = 'ALTERNATIVE NAMES'
条件,而不是where Clause
SELECT products.code, products.name, product_details.product_code, product_details.detail_type, product_details.description
FROM products
LEFT OUTER JOIN product_details
ON products.code = product_details.product_code
and product_details.detail_type = 'ALTERNATIVE NAMES'
只需将AND
替换为WHERE
即可完全使用LEFT [OUTER] JOIN
。
例如
ON products.code = product_details.product_code
AND product_details.detail_type = 'ALTERNATIVE NAMES'
否则查询将被视为INNER JOIN
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.