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
Try an explicit
JOINcondition and explicitGROUP BY(although mysql allows to skip on the later).Answer to additional question:
Again, try proper SQL syntax whit explicit
JOINand join-conditions:This way you notice immediately that the table
wellsis 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.*).