表格结构如下:
planID TinyInt(1)
userID INT(11)
isComplete TinyInt(1)
isQuit TinyInt(1)
所以,首先,我需要:
因此,ID为1的用户可以加入计划3 3次,1次完成2次退出,1次加入计划2次,2次完成等....
以某种方式尝试
SELECT COUNT(`isComplete`) as total_complete, COUNT(`isQuit`) as total_quit FROM `user_plan` WHERE `userID` = 1 AND isComplete = 1 OR isQuit = 1 GROUP BY `planID`
但与预期不一样
如果它太复杂,我可以为每个计划查询3次,感谢您的帮助
已更新 样本输入:
planID | userID | isComplete | isQuit
-------------------------------------------------------
1 | 1 | 0 | 1
1 | 1 | 1 | 0
1 | 1 | 0 | 0
答案 0 :(得分:2)
如果案例是在SQL Server中,那就是这样的。 (如果读的是正确的问题)
SELECT userID,
SUM(isComplete) as total_complete,
SUM(isQuit) as total_quit
FROM user_plan
WHERE userID = 1
AND isComplete = 1
GROUP BY userID
答案 1 :(得分:0)
这是Jithin Shaji回答的启发,感谢再次帮助。
SELECT SUM(isComplete) as total_complete,
SUM(isQuit) as total_quit,
planID
FROM user_plan
WHERE userID = 31
GROUP BY planID