我的数据库有3行(并非列出所有列):
**uFirst** | **uApp**
Dave | approved
John | declined
Jane | no
此SQL正在运行,并显示所有行:
SELECT u.uID, u.uManT, u.uFirst AS 'NomFN', m.uFirst AS 'ManFN', u.uLast AS 'NomLN', m.uLast AS 'ManLN', u.uNomd, u.uApp, u.uApp, u.uAppd, u.uRep, u.uRepd, u.uManT, u.uStatus, u.uManR
FROM users AS u LEFT OUTER JOIN users AS m
ON u.uManT = m.uID
WHERE u.uManR = " . $luID . "
AND u.uStatus = 'Physician'
ORDER BY u.uLast ASC";
当我尝试添加COUNT时,它只显示1行(已批准的行):
SELECT COUNT(CASE WHEN u.uApp = 'approved' THEN 1 END) AS Approved,
COUNT(CASE WHEN u.uApp = 'declined' THEN 1 END) AS Declined,
u.uID, u.uManT, u.uFirst AS 'NomFN', m.uFirst AS 'ManFN', u.uLast AS 'NomLN', m.uLast AS 'ManLN', u.uNomd, u.uApp, u.uApp, u.uAppd, u.uRep, u.uRepd, u.uManT, u.uStatus, u.uManR
FROM users AS u LEFT OUTER JOIN users AS m
ON u.uManT = m.uID
WHERE u.uManR = " . $luID . "
AND u.uStatus = 'Physician'
ORDER BY u.uLast ASC";
我正在尝试显示所有行,以及已批准的用户数。我做错了什么?
答案 0 :(得分:0)
在这种情况下只返回1行,因为你没有使用GROUP BY
,这个查询可以得到你想要的东西:
SELECT COUNT(uFirst) AS Total, u.uID, u.uManT,
u.uFirst AS 'NomFN', m.uFirst AS 'ManFN', u.uLast AS 'NomLN',
m.uLast AS 'ManLN', u.uNomd, u.uApp, u.uApp, u.uAppd, u.uRep,
u.uRepd, u.uManT, u.uStatus, u.uManR
FROM users AS u
LEFT OUTER JOIN users AS m ON u.uManT = m.uID
WHERE u.uManR = " . $luID . " AND u.uStatus = 'Physician'
GROUP BY uApp
ORDER BY u.uLast ASC";