单个mysql查询中的多个计数

时间:2013-08-23 11:32:34

标签: mysql

如何在mysql中对同一个字段进行多次计数?下面的单个计数代码可以正常工作

SELECT fruit, COUNT(DISTINCT site) AS `apple` FROM grocery where fruit like '%06201%'

然而,当我尝试过这个但是我得到语法错误

SELECT
    SUM(fruit like '%06201%') AS `apple`,
    SUM(fruit like '%02206%') AS `pears`,
FROM grocery

2 个答案:

答案 0 :(得分:3)

SELECT
    SUM(fruit like '%06201%') AS `apple`,
    SUM(fruit like '%02206%') AS `pears`,
                                        ^
                                        here
FROM grocery

你有两个逗号,但你只需要一个。

答案 1 :(得分:1)

FROM

之前删除额外的逗号
SELECT
    SUM(fruit like '%06201%') AS `apple`,
    SUM(fruit like '%02206%') AS `pears`
FROM grocery