用双组查询?

时间:2013-09-23 12:48:32

标签: mysql

我正在尝试编辑此查询:

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"}

我该怎么办?

1 个答案:

答案 0 :(得分:4)

要完成此操作,您只需将banner添加到SELECT列表和GROUP BY。但是,它必须位于dayofweek中的GROUP BY表达式之前。

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

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