0

我的表有 3 个字段,我想得到我里面重复次数最多的ticket_id,这个查询有效,但我需要在“is_winner”上添加一个 where 子句,有什么帮助吗?谢谢

 ID   TICKET_ID   IS_WINNER
 1    2           1
 2    3           1
 3    2           1
 4    3           0
 5    2           1
 6    2           0
 7    3           0
 8    2           1
 9    2           0


 select query1.* FROM (Select ticket_id, Count(*)  As order_count 
 FROM ticket_log  
 GROUP BY ticket_log.ticket_id) query1,(Select Max(query2.order_count) As highest_count 
 FROM (Select ticket_id, Count(*) As order_count 
 FROM ticket_log 
 GROUP BY ticket_log.ticket_id) query2) query3 where query1.order_count = query3.highest_count
4

2 回答 2

1
SELECT ticket_id, COUNT() AS wins
FROM ticket_log
WHERE is_winner=1
GROUP BY ticket_id
ORDER BY wins DESC;

这是?

于 2013-10-02T10:42:03.233 回答
1

只需添加 WHERE 子句:

SELECT query1.* FROM (Select ticket_id, Count(*)  As order_count 
FROM ticket_log WHERE is_winner = 1
GROUP BY ticket_log.ticket_id) query1,(Select Max(query2.order_count) As highest_count 
FROM (Select ticket_id, Count(*) As order_count 
FROM ticket_log WHERE is_winner = 1
GROUP BY ticket_log.ticket_id) query2) query3 where query1.order_count = query3.highest_count

(编辑是因为我注意到你需要所有的获胜者)

于 2013-10-02T10:46:52.630 回答