如何在sql中合并多个where(卡住)

yb3bgrhw  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(291)

如何将这两个数据合并在一起得到这样的数据

SELECT A.schlvl AS School, COUNT(DISTINCT A.name) AS NoOfChild ,SUM(B.cat_id=1) AS Picture 
FROM child AS A INNER JOIN
question AS B 
ON A.child_id=B.child_id
WHERE B.answer=0 AND B.cat_id=1
GROUP BY A.schlvl

SELECT COUNT(DISTINCT A.name) AS Gotitright ,SUM(B.cat_id=2) AS Letters
FROM child AS A INNER JOIN
question AS B 
ON A.child_id=B.child_id
WHERE B.answer = 0 AND B.cat_id = 2
GROUP BY A.schlvl

或者
像这样的事,但我被这个困住了

SELECT A.schlvl AS School, COUNT(DISTINCT A.name WHERE B.answer=0 AND B.cat_id=1) AS Picture , COUNT(DISTINCT A.name WHERE B.answer=0 AND B.cat_id=2) AS Letters
FROM child AS A INNER JOIN
question AS B 
ON A.child_id=B.child_id
GROUP BY A.schlvl
cwdobuhd

cwdobuhd1#

从你的询问中。
你可以试着用 INB.cat_id 得到
cat_id 1 或者 2WHERE 条款。
使用聚合函数条件let CASE WHENSUM 功能。

SELECT A.schlvl AS School, COUNT(DISTINCT CASE WHEN B.cat_id = 1 THEN A.name END) AS NoOfChild ,SUM(CASE WHEN B.cat_id IN (1,2) THEN 1 END) AS Picture 
FROM 
child AS A INNER JOIN question AS B ON A.child_id=B.child_id
WHERE B.answer=0 AND B.cat_id= in (1,2)
GROUP BY A.schlvl

相关问题