通常我对SQL查询非常有信心,但是这个让我摸不着头脑.我觉得这应该是一个快速修复,但我只是没有看到它.
我试图在一个查询中对同一个表上的多个值进行计数.
不要介意“0000000000000000”,它只是表示一个空字节数组.
SELECT COUNT(ssn)
FROM patients
WHERE ssn="0000000000000000";
SELECT COUNT(firstname)
FROM patients
WHERE firstname="0000000000000000"
SELECT COUNT(lastname)
FROM patients
WHERE lastname="0000000000000000"
etc...
最佳答案
你可以这样做 –
原文链接:https://www.f2er.com/mysql/433386.htmlSELECT COUNT(ssn) AS patient_count,'ssn' AS count_type
FROM patients
WHERE ssn="0000000000000000";
UNION
SELECT COUNT(firstname) AS patient_count,'firstname' AS count_type
FROM patients
WHERE firstname="0000000000000000"
UNION
SELECT COUNT(lastname) AS patient_count,'lastname' AS count_type
FROM patients
WHERE lastname="0000000000000000"