简体   繁体   中英

How to use INNER JOIN?

I am new in PHP. I have a sql query. I use 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 just selects all from the first table specified, and fills fields from other tables - where no entry is found - with NULL .

Just replace all your INNER JOIN with LEFT JOIN .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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