我的查询如下:
SELECT cp.p_id AS p_id
,cp.p_id AS pk
,cp.name_first AS name_first
,cp.name_last AS name_last
[...]
FROM tbl_E AS cp
INNER JOIN tbl_B, tbl_C, tbl_A, tbl_D AS cpr
WHERE cp.p_id = tbl_C.p_id
AND tbl_C.sp_id = tbl_A.vsp_id
AND tbl_B.p_id = cp.p_id
AND cpr.pr_id = tbl_A.pr_id
AND (
cp.p_id = 339066
OR cp.p_id = 103688
OR cp.p_id = 321496
)
GROUP BY cp.p_id
ORDER BY name_last ASC LIMIT 0,1000;
我希望得到3行,但我得到的行少于3行,因为tbl_C.p_id不包含所有的cp.p_id。如何更改查询以获得正好3行?
感谢。
答案 0 :(得分:0)
这是因为您正在进行INNER JOIN
...将其更改为LEFT OUTER JOIN
,如下所示
SELECT cp.p_id AS p_id
,cp.p_id AS pk
,cp.name_first AS name_first
,cp.name_last AS name_last
[...]
FROM tbl_E AS cp
LEFT JOIN tbl_B ON tbl_B.p_id = cp.p_id
LEFT JOIN tbl_C ON cp.p_id = tbl_C.p_id
LEFT JOIN tbl_A ON tbl_C.sp_id = tbl_A.vsp_id
LEFT JOIN tbl_D AS cpr ON cpr.pr_id = tbl_A.pr_id
WHERE
cp.p_id IN (339066,103688,321496)
GROUP BY cp.p_id
ORDER BY cp.name_last
LIMIT 0,1000;