简体   繁体   English

如何使用INNER JOIN?

[英]How to use INNER JOIN?

I am new in PHP. 我是PHP新手。 I have a sql query. 我有一个SQL查询。 I use INNER Join. 我使用INNER Join。 I face some problem in it. 我面临一些问题。 If there is any single blank field is empty it does not display any data of other field. 如果有一个空白字段为空,则不会显示其他字段的任何数据。

Here is my Query 这是我的查询

$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 ";

Therefore, you have LEFT JOIN (or RIGHT JOIN in some special cases). 因此,您具有LEFT JOIN (在某些特殊情况下为RIGHT JOIN )。 LEFT JOIN just selects all from the first table specified, and fills fields from other tables - where no entry is found - with NULL . LEFT JOIN只是从指定的第一个表中选择所有内容,并用NULL填充其他表(未找到任何条目)中的字段。

Just replace all your INNER JOIN with LEFT JOIN . 只需将所有INNER JOIN替换为LEFT JOIN

Replace INNER JOIN with 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