在 MYSQL 中使用 COUNT 后仅一行
不知道为什么我只得到一行计数错误!
以下是工作代码:
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
每个子项目都有多个表,我基本上试图查看其中一个表中的列是否有包含非空值的行,然后在每个子项目的另一列中的子项目旁边显示该计数。
以下是我查询以下语句时得到的结果:
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 的所有行,没有其他内容。
这里出了什么问题?
编辑:
埃尔文的解决方案解决了这个问题。但我的表中没有 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
Not sure why I am getting just one row with wrong count!
Following is the working code:
SELECT project_name, sub_project_name
FROM projects, sub_projects
WHERE projects.project_id = sub_projects.projects_project_id
Result:
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
Each sub_project has multiple tables and I am basically trying to see if a column in one of the tables has any row containing non-Null values and then show that count beside the sub_project in another column for each sub_project.
Here is what I get when I query the following statement:
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
Result:
project_name sub_project_name porosity
Bakken Ghost fracture Study 99
All the rows from every sub_project in the first row and nothing else.
Whats wrong here?
Edit:
Erwin's solution nailed it. But I do not have sub_project_id
in my tables. I just added it in one table to make it simpler for testing.
So I used Erwin's GROUP BY
suggestion to edit my statement and I get the right matrix shape but not the right counts. Counts are way off.
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
Result:
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
The correct result should be:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试显式的
JOIN
条件和显式的GROUP BY
(尽管mysql允许跳过后者)。回答附加问题:
再次,尝试使用显式
JOIN
和联接条件的正确 SQL 语法:这样您会立即注意到表
wells
已无条件联接。这会导致交叉连接:左侧的每一行都与右侧的每一行一起扩展,从而产生很多行。可能是您计数过多的根源。另请注意,count 仅计算非空值。 bl.bl_por IS NULL 的任何行均不计算在内。如果您确实想计算行数,可以使用
count(bl.*)
。Try an explicit
JOIN
condition and explicitGROUP BY
(although mysql allows to skip on the later).Answer to additional question:
Again, try proper SQL syntax whit explicit
JOIN
and join-conditions:This way you notice immediately that the table
wells
is joined in without unconditionally. This results in a cross join: every row of the left side is extended with every row on the right side, which produces a lot of rows. Probably the source of your excessive counts.Also be aware that count only counts non-null values. Any row where bl.bl_por IS NULL is not counted. If you actually want to count the number of rows, you can use
count(bl.*)
.