WHERE 子句忽略 LEFT OUTER JOIN 中的行
WHERE clause ignoring rows in LEFT OUTER JOIN
我有三个table:
clinic = (id,name,short_name,region,country,continent,area)
result_month =(id,year,result_quarter_id,month)
test_value_in_range_count =(clinic_id,result_month_id,patient_sub_set_id,test_value_range_id,number_of_values)
示例数据:
诊所
id region country continent area
3299 Kazakhstan Kazakhstan Eurasia Middle East/Asia
result_month
id year result_quarter_id month
200801 2008 2008Q1 1
test_value_in_range_count
诊所 table 中没有诊所 ID 3299 的数据。但是 JOINS 必须 return
我需要 result_month table 中的所有行都包含 test_value_in_range_count 中的空行.问题是 WHERE
子句。这将停止生成行,因为从 result_month 到 test_value_range_id 的某些行显然不存在数据。
预期结果
clinic region country continent area ym vf
3299 Kazakhstan Kazakhstan Eurasia Middle East/Asia 201511 null
我尝试了很多不同的查询,将它们分开但没有成功。任何帮助或指导将不胜感激。
SELECT
apc.id AS clinic,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id AS ym,
SUM(CASE test_value_range_id WHEN '1124_1' THEN number_of_values ELSE 0 END) AS avf
FROM result_month vrm
LEFT JOIN test_value_in_range_count vt on vrm.id = vt.result_month_id
LEFT OUTER JOIN clinic apc on vt.clinic_id = apc.id
WHERE (vt.test_value_range_id IN ('1124_1', '1124_2', '1124_3', '1124_4', '1124_5')) AND (vt.patient_sub_set_id = 'ALL')
GROUP BY apc.id,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id
;
从 where 子句中删除条件并将其添加到 join
:
SELECT
apc.id AS clinic,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id AS ym,
SUM(CASE test_value_range_id WHEN '1124_1' THEN number_of_values ELSE 0 END) AS avf
FROM result_month vrm
LEFT JOIN test_value_in_range_count vt on vrm.id = vt.result_month_id and (vt.test_value_range_id IN ('1124_1', '1124_2', '1124_3', '1124_4', '1124_5')) AND (vt.patient_sub_set_id = 'ALL')
LEFT OUTER JOIN clinic apc on vt.clinic_id = apc.id
GROUP BY apc.id,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id
;
否则 where 子句从您的 left join
生成 join
我有三个table:
clinic = (id,name,short_name,region,country,continent,area)
result_month =(id,year,result_quarter_id,month)
test_value_in_range_count =(clinic_id,result_month_id,patient_sub_set_id,test_value_range_id,number_of_values)
示例数据:
诊所
id region country continent area
3299 Kazakhstan Kazakhstan Eurasia Middle East/Asia
result_month
id year result_quarter_id month
200801 2008 2008Q1 1
test_value_in_range_count
诊所 table 中没有诊所 ID 3299 的数据。但是 JOINS 必须 return
我需要 result_month table 中的所有行都包含 test_value_in_range_count 中的空行.问题是 WHERE
子句。这将停止生成行,因为从 result_month 到 test_value_range_id 的某些行显然不存在数据。
预期结果
clinic region country continent area ym vf
3299 Kazakhstan Kazakhstan Eurasia Middle East/Asia 201511 null
我尝试了很多不同的查询,将它们分开但没有成功。任何帮助或指导将不胜感激。
SELECT
apc.id AS clinic,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id AS ym,
SUM(CASE test_value_range_id WHEN '1124_1' THEN number_of_values ELSE 0 END) AS avf
FROM result_month vrm
LEFT JOIN test_value_in_range_count vt on vrm.id = vt.result_month_id
LEFT OUTER JOIN clinic apc on vt.clinic_id = apc.id
WHERE (vt.test_value_range_id IN ('1124_1', '1124_2', '1124_3', '1124_4', '1124_5')) AND (vt.patient_sub_set_id = 'ALL')
GROUP BY apc.id,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id
;
从 where 子句中删除条件并将其添加到 join
:
SELECT
apc.id AS clinic,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id AS ym,
SUM(CASE test_value_range_id WHEN '1124_1' THEN number_of_values ELSE 0 END) AS avf
FROM result_month vrm
LEFT JOIN test_value_in_range_count vt on vrm.id = vt.result_month_id and (vt.test_value_range_id IN ('1124_1', '1124_2', '1124_3', '1124_4', '1124_5')) AND (vt.patient_sub_set_id = 'ALL')
LEFT OUTER JOIN clinic apc on vt.clinic_id = apc.id
GROUP BY apc.id,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id
;
否则 where 子句从您的 left join
join