我正在尝试编辑此查询:
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"}
我该怎么办?
答案 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将在那里报告错误。