不知道为什么我只得错误统计一行!
以下是工作代码:
SELECT project_name, sub_project_name
FROM projects, sub_projects
WHERE projects.project_id = sub_projects.projects_project_id
结果:
project_name sub_project_name
Bakken Ghost fracture Study
Bakken Bakken Mylo QAQC
Bossier Doyle Boles K No.1
Eagleford Kennedy Unit#1H
Eagleford Wehmeyer Unit #1
Niobrara Crow Valley 7-62-32-1M
Poland Poland
Woodford Ridenour Phase 2
Woodford Teague 1-14H
每个sub_project都有多个表,我基本上试图查看其中一个表中的列是否包含任何包含非Null值的行,然后在每个sub_project的另一列中的sub_project旁边显示该计数。
以下是我查询以下语句时得到的内容:
SELECT projects.project_name, sub_projects.sub_project_name, COUNT( bl.bl_por ) AS porosity
FROM projects, sub_projects
LEFT JOIN bl ON sub_projects.sub_project_id = bl.sub_project_id
WHERE projects.project_id = sub_projects.projects_project_id
结果:
project_name sub_project_name porosity
Bakken Ghost fracture Study 99
第一行中每个sub_project的所有行,没有别的。
这里有什么问题?
Erwin的解决方案钉了它。但是我的表中没有sub_project_id
。我只是将它添加到一个表中,以便更容易进行测试。
所以我使用Erwin的GROUP BY
建议来编辑我的陈述,我得到了正确的矩阵形状但不是正确的数字。计算结束了。
SELECT p.project_name, sp.sub_project_name, COUNT( bl.bl_por ) AS porosity
FROM projects p, sub_projects sp, wells w, cores c, samples s, inputs i
LEFT JOIN bl ON i.inputs_id = bl.inputs_inputs_id
WHERE p.project_id = sp.projects_project_id
AND s.sample_id = i.samples_sample_id
AND c.core_id = s.cores_core_id
AND sp.sub_project_id = c.sub_projects_has_wells_sub_projects_sub_project_id
GROUP BY p.project_name, sp.sub_project_name
结果:
project_name sub_project_name porosity
Bakken Bakken Mylo QAQC 147
Bakken Ghost fracture Study 252
Bossier Doyle Boles K No.1 189
Eagleford Kennedy Unit#1H 294
Eagleford Wehmeyer Unit #1 0
Niobrara Crow Valley 7-62-32-1M 0
Poland Poland 714
Woodford Ridenour Phase 2 483
Woodford Teague 1-14H 0
正确的结果应该是:
Bakken Bakken Mylo QAQC 7
Bakken Ghost fracture Study 12
Bossier Doyle Boles K No.1 9
Eagleford Kennedy Unit#1H 14
Eagleford Wehmeyer Unit #1 0
Niobrara Crow Valley 7-62-32-1M 0
Poland Poland 34
Woodford Ridenour Phase 2 23
Woodford Teague 1-14H 0
答案 0 :(得分:2)
尝试显式JOIN
条件和显式GROUP BY
(尽管mysql允许稍后跳过)。
SELECT p.project_name, sp.sub_project_name, COUNT(bl.bl_por) AS porosity
FROM projects p
JOIN sub_projects sp ON p.project_id = sp.projects_project_id
LEFT JOIN bl ON sp.sub_project_id = bl.sub_project_id
GROUP BY p.project_name, sp.sub_project_name;
再次尝试使用正确的SQL语法whit explicit JOIN
和join-conditions:
SELECT p.project_name, sp.sub_project_name, COUNT(bl.bl_por) AS porosity
FROM projects p
JOIN sub_projects sp ON p.project_id = sp.projects_project_id
JOIN wells w -- no JOIN condition? Results in cross join.
JOIN cores c ON sp.sub_project_id = c.sub_projects_has_wells_sub_projects_sub_project_id
JOIN samples s ON c.core_id = s.cores_core_id
JOIN inputs i ON s.sample_id = i.samples_sample_id
LEFT JOIN bl ON i.inputs_id = bl.inputs_inputs_id
GROUP BY p.project_name, sp.sub_project_name
通过这种方式,您可以立即注意到表wells
无条件地加入。这会导致交叉连接:左侧的每一行都会延伸到右侧的每一行,从而产生很多行。可能是过度计数的来源。
另请注意,计数仅计算非空值。 bl.bl_por IS NULL的任何行都不计算在内。如果您确实要计算行数,可以使用count(bl.*)
。