[英]Combining two Count Queries into one
I have theses two queries 我有两个问题
SELECT course,
COUNT(*) AS countp
FROM table1
WHERE pickout='Yes'
GROUP BY course
SELECT course,
COUNT(*) AS countw
FROM table1
WHERE pickout='Yes' AND result='Won'
GROUP BY course
what I am trying to achieve is a table with three columns Course
, Countp
, Countw
but I am having trouble combining the two into one query. 我想要实现的是一个具有三列Course
, Countp
, Countw
的表,但是我很难将两者合并为一个查询。
Basically I am looking for a list of course with number of picks and then number of wins. 基本上,我正在寻找选秀次数和获胜次数的课程列表。
In MySQL the result of conditions evaluate to 1
or 0
. 在MySQL中,条件的结果评估为1
或0
。 You can sum that up 你可以总结一下
SELECT course,
sum(pickout='Yes') AS countp,
sum(pickout='Yes' AND result='Won') AS countw
FROM table1
GROUP BY course
Try the following SQL: 尝试以下SQL:
SELECT
Course,
COUNT(*) AS CountP,
SUM(CASE WHEN result='Won' THEN 1 ELSE 0 END) AS CountW
FROM table1
WHERE pickout = 'Yes'
GROUP BY Course
try this: 尝试这个:
select a.course,a.countp ,b.countw from (SELECT course, COUNT(*) AS countp FROM table1 WHERE pickout='Yes' GROUP BY course)a full join
(SELECT course, COUNT(*) AS countw FROM table1 WHERE pickout='Yes' AND result='Won' GROUP BY course)b
a.course=b.course
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.