版本:mySQLi PHP 5.4.45
我正在尝试计算每个唯一ID的面板数。相反,我得到每个ID的测试总数。
查询前:(示例)
eReq | panel_name | test
------------------------
500 | tox | Butabital
500 | tox | Amphetamine
500 | tox | MethAmp
500 | chem | CBC
600 | tox | Morphine
600 | chem | CBC
查询:
SELECT panel_name, count(*)
FROM `ordered_codes`
JOIN samples ON ordered_codes.tox_id = samples.tox_id
JOIN orders_codes ON ordered_codes.code_id = orders_codes.id
GROUP BY panel_name
当前结果:
panel_name | Count(*)
---------------------
tox | 4
chem | 2
所需结果:(数字应低一些-按eReq计数panel_name)
panel_name | Count(*)
---------------------
tox | 2
chem | 2
我觉得这很简单,在这一点上我只是想得太多。
答案 0 :(得分:0)
您要寻找count(distinct)
吗?
SELECT panel_name, count(distinct ereq)
FROM ordered_codes oc JOIN
samples s
ON oc.tox_id = s.tox_id
GROUP BY panel_name
我不明白为什么要对ordered_codes
进行两次联接,所以我删除了第二个联接,并假设这是一个错字。您的查询将生成语法错误。