SQL COUNT()在MySQL中不起作用

时间:2011-10-11 16:27:49

标签: mysql sql

SELECT person.id,
       person.name,
       COUNT(DISTINCT fruit.apple) AS "Red Apple",
       fruit.*
FROM   (SELECT *
        FROM   tree
        ORDER  BY color DESC) AS fruit
       INNER JOIN person
         ON fruit.id = person.id
WHERE  person.name = 'John Smith'  

现在,在我放入COUNT(DISTINCT fruit.apple) AS "Apple"之前,该代码正在运行。这有什么问题?

提前致谢

2 个答案:

答案 0 :(得分:4)

您没有group by条款。

答案 1 :(得分:0)

试试这个

SELECT person.id,
       person.name,
       COUNT(DISTINCT fruit.apple) AS "Red Apple",
       fruit.*
FROM   (SELECT *
        FROM   tree
        ORDER  BY color DESC) AS fruit
       INNER JOIN people
         ON fruit.id = person.id
WHERE  person.name = 'John Smith' 
GROUP BY fruit.apple