错误 - 选择内部计数

时间:2014-09-28 16:37:56

标签: mysql sql

我有这个程序

SELECT COUNT( a.id_badania ) AS iloscBadan, COUNT( b.zdjecie ) AS iloscZdjec, COUNT (SELECT id_badania FROM Badania WHERE status='W trakcie realizacji';)
FROM Badania a
INNER JOIN Zdjecia b ON a.id_badania = b.id_badania

这个错误

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'SELECT id_badania FROM Badania WHERE status ='W trakcie realizacji'附近使用正确的语法。来自Badani'的第1行

我不太了解sql。

2 个答案:

答案 0 :(得分:1)

您可以使用sum和case语句来计算特定状态的值

    SELECT COUNT( a.id_badania )   AS iloscBadan, COUNT( b.zdjecie ) AS iloscZdjec, Sum( case when status='W trakcie realizacji' then 1 else 0 end ) 
     FROM Badania a
     INNER JOIN Zdjecia b ON a.id_badania = b.id_badania

答案 1 :(得分:1)

您不能将子查询放在聚合函数中。并且在查询中间有一个分号。我猜你只想要有条件的聚合。要么:

SELECT COUNT( a.id_badania ) AS iloscBadan, COUNT( b.zdjecie ) AS iloscZdjec,
       SUM(status = 'W trakcie realizacji')
FROM Badania a INNER JOIN
     Zdjecia b
     ON a.id_badania = b.id_badania;

或者:

SELECT COUNT( a.id_badania ) AS iloscBadan, COUNT( b.zdjecie ) AS iloscZdjec,
       COUNT(DISTINCT case when status = 'W trakcie realizacji' then a.id_badania end)
FROM Badania a INNER JOIN
     Zdjecia b
     ON a.id_badania = b.id_badania;