我有一个简单的(不实用的)表格
STUDENTID SERVICE
1 a
1 b
1 c
1 d
1 e
2 a
2 b
2 c
2 d
2 e
3 a
3 b
3 c
4 a
4 f
5 a
5 d
6 f
6 g
7 a
7 b
7 c
7 d
7 e
8 a
8 b
8 c
8 d
8 e 例如,我想将某些信息列成表格。
有多少学生注册了服务'a‘和服务'b’。
有多少学生注册了服务'a‘和服务'c’。
有多少学生注册了服务'a‘和服务'd’。等。
或
有多少学生注册了服务“a”和除“a”之外的另一项服务。
有多少学生注册了服务“b”和除“b”之外的其他两项服务。
服务的数量可能会在未来发生变化,但目前还可以。
这就是我现在所拥有的,但它不起作用。
--想要返回注册了a和1任何其他服务的学生的数量
select COUNT(STUDENTID), service from table
group by service where service = 'a' and studentid in
(select studentid from table group by STUDENTID having COUNT(service) = 2) 发布于 2012-08-22 23:55:48
这里有一些关于你的问题的提示。
仅服务'a‘和服务'b’就注册了多少学生。
SELECT COUNT(*)
FROM
(
SELECT studentID
FROM tableName
WHERE Service IN ('A', 'B')
GROUP BY StudentID
HAVING COUNT(studentID) = 2
) a仅服务'a‘和服务'c’就注册了多少学生。
SELECT COUNT(*)
FROM
(
SELECT studentID
FROM tableName
WHERE Service IN ('A', 'C')
GROUP BY StudentID
HAVING COUNT(studentID) = 2
) a发布于 2012-08-22 23:42:32
下面是什么:
select COUNT(t.STUDENTID)
from
(select studentid, service from table group by STUDENTID having COUNT(service) = 2) as t
WHERE t.service = 'a'发布于 2012-08-22 23:46:20
你可以自己加入
SELECT COUNT(DISTINCT studentid) n
FROM table t1
LEFT JOIN table t2 on (t1.studentid = t2.studentid)
WHERE t1.service = 'a' AND t2.service <> 'a'或者如果你想了解学生..。
SELECT studentid, SUM(IF(service = 'a',1,0)) n_a, SUM(IF(service = 'a',0,1)) n_other
FROM table
GROUP BY studentid
HAVING n_a = 1 AND n_other >= 1https://stackoverflow.com/questions/12076693
复制相似问题