6

我正在尝试编辑此查询:

SELECT (WEEKDAY( date ) +1) dayofweek, COUNT( * ) count 
FROM abanners 
WHERE iid = ? AND banner LIKE ? 
GROUP BY (WEEKDAY( date ) +1) 
ORDER BY count DESC

通过这个查询,我得到了按星期几分割的条目数,但是如果我需要同样的东西也分割banner呢?

所以我想拥有:

 banner1: entries splitted by day of week
 banner2: entries splitted by day of week

 ex: {"banner1" : "1,3,4,1,3,5,1", "banner2": "1,3,4,1,3,5,1"}

我能怎么做?

4

1 回答 1

7

为此,您只需要同时添加bannerSELECT列表和GROUP BY. 但是,它必须出现dayofweekGROUP BY.

SELECT 
  banner,
  (WEEKDAY( date ) +1) dayofweek,
  COUNT(*) count
FROM abanners
WHERE 
  iid = ? 
  AND banner LIKE ?
GROUP BY
  banner,
  dayofweek
ORDER BY count DESC

请注意,我dayofweekGROUP BY这里使用了别名。MySQL 允许在GROUP BY其他一些 RDBMS 将在那里报告错误的地方使用别名。

于 2013-09-23T13:22:32.930 回答