我将MYSQL表名称中的下表作为带有列
的a_ad_display╔═════════════╦═════════╦═════════╦═════════════════════╗
║ addisplayId ║ restId ║ clicked ║ displaytime ║
╠═════════════╬═════════╬═════════╬═════════════════════╣
║ 1 ║ 20707 ║ 0 ║ 2013-02-28 10:50:55 ║
║ 2 ║ 20708 ║ 1 ║ 2013-02-28 10:55:55 ║
║ 3 ║ 20711 ║ 1 ║ 2013-03-01 12:50:55 ║
║ 4 ║ 20712 ║ 1 ║ 2013-03-02 01:50:55 ║
║ 5 ║ 20707 ║ 1 ║ 2013-03-03 10:50:55 ║
║ 6 ║ 20708 ║ 1 ║ 2013-03-03 12:50:55 ║
║ 7 ║ 20707 ║ 1 ║ 2013-03-04 09:50:55 ║
║ 8 ║ 20708 ║ 1 ║ 2013-03-05 10:50:55 ║
║ 9 ║ 20708 ║ 0 ║ 2013-03-06 11:50:55 ║
║ 10 ║ 20707 ║ 0 ║ 2013-03-06 12:50:55 ║
╚═════════════╩═════════╩═════════╩═════════════════════╝
现在我想要获取restId,它在Clicked
列中具有最高的并发值1所以从上面的记录中,20708 restId具有最高的点击列数,即3 ..所以我想用这个场景获取restId所以任何人都可以指导我,我该怎么做?
答案 0 :(得分:1)
这是最简单的答案,但它不会处理重复,
SELECT restID, SUM(clicked) totalClick
FROM a_ad_display
GROUP BY restID
ORDER BY totalClick DESC
LIMIT 1
这是将保留重复的查询,
SELECT restID, SUM(clicked) totalClick
FROM a_ad_display
GROUP BY restID
HAVING SUM(clicked) =
(
SELECT SUM(clicked) totalClick
FROM a_ad_display
GROUP BY restID
ORDER BY totalClick DESC
LIMIT 1
)