1

如何在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
4

2 回答 2

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

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

于 2013-08-23T11:35:03.580 回答
1

去掉前面多余的逗号FROM

SELECT
    SUM(fruit like '%06201%') AS `apple`,
    SUM(fruit like '%02206%') AS `pears`
FROM grocery
于 2013-08-23T11:37:39.527 回答