繁体   English   中英

如何使用INNER JOIN?

[英]How to use INNER JOIN?

我是PHP新手。 我有一个SQL查询。 我使用INNER Join。 我面临一些问题。 如果有一个空白字段为空,则不会显示其他字段的任何数据。

这是我的查询

$sql= "SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as cname,t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle
FROM og_ratings r 
INNER JOIN og_companies c
ON r.client_id = c.id
INNER JOIN og_rating_types t
ON r.rating_type_id = t.id
INNER JOIN og_actions a
ON r.pacra_action = a.id
INNER JOIN og_outlooks o
ON r.pacra_outlook = o.id
INNER JOIN og_lterms l
ON r.pacra_lterm = l.id
INNER JOIN og_sterms s
ON r.pacra_sterm = s.id
WHERE c.id= '$id2'
ORDER BY r.id DESC
LIMIT 1 ";

因此,您具有LEFT JOIN (在某些特殊情况下为RIGHT JOIN )。 LEFT JOIN只是从指定的第一个表中选择所有内容,并用NULL填充其他表(未找到任何条目)中的字段。

只需将所有INNER JOIN替换为LEFT JOIN

LEFT JOIN替换INNER JOIN LEFT JOIN

$sql= "SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as cname,t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle
FROM og_ratings r 
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
WHERE c.id= '$id2'
ORDER BY r.id DESC
LIMIT 1 ";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM