我如何将这两个数据合并在一起以获取数据
this

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

最佳答案

从您的查询。

您可以尝试在IN中使用B.cat_idcat_id子句中获取1 2WHERE

使用聚集函数条件让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

关于mysql - 如何在SQL(STUCK)中合并多个WHERE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51692379/

10-17 03:08