如何使用INNER JOIN?
How to use INNER JOIN?
我是 PHP 的新人。我有一个 sql 查询。我使用内部连接。我遇到了一些问题。如果有任何一个空白字段为空,则不会显示其他字段的任何数据。
这是我的查询
$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
仅从指定的第一个 table 中选择所有内容,并使用 NULL
.
填充其他 table 中未找到条目的字段
只需将所有 INNER 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 ";
我是 PHP 的新人。我有一个 sql 查询。我使用内部连接。我遇到了一些问题。如果有任何一个空白字段为空,则不会显示其他字段的任何数据。
这是我的查询
$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
仅从指定的第一个 table 中选择所有内容,并使用 NULL
.
只需将所有 INNER 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 ";