我有三个 MySQL 表:
patient:
paID, paCode, paAccountID (foreign key)
test
tsID, tsName, tsPatientID (foreign key), tsAccountID (foreign key)
account
acID etc.
现在我想计算paID
链接tsID
到特定acID=1
. 有 6tsID
和 4paID
链接到acID=1
.
SELECT Count(paID) AS paCount FROM patient WHERE paAccountID=1
SELECT Count(tsID) AS tsCount FROM test WHERE tsAccountID=1
试图将两者都纳入一个查询...
SELECT Count(tsID) AS tsCount, Count(paID) AS paCount
FROM test LEFT JOIN patient ON tsPatientID = paID
WHERE tsAccountID=1
那样不行,两个计数都返回 6。如何正确处理?