2013-03-11 66 views
1

我有如下的MySQL表名稱表作爲a_ad_display的列如何獲取表中具有最高布爾列數的記錄的Id?

╔═════════════╦═════════╦═════════╦═════════════════════╗ 
║ 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其中有1最高concurrences中單擊的列

從上面

所以記錄20708個restId有最高點擊的列數是3 ..所以我想獲取restId與這種情況..任何人都可以請指導我,我該怎麼做?

回答

1

這是simpliest答案,但它不會處理重複,

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 
     ) 
+0

保持重複的意思是? – 2013-03-11 05:54:53

+0

@BhavikKama看到這個例子,當多個'restID'的總點擊次數是相同的,並且恰好是最高的點擊次數,例如[>> CLICK_HERE_FOR_DEMO <<](http://www.sqlfiddle.com/#! 2/7017b/2) – 2013-03-11 06:01:18

+0

哦,好吧..非常感謝你...... – 2013-03-11 06:07:17

相關問題