我的表有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
答案 0 :(得分:1)
SELECT ticket_id, COUNT() AS wins
FROM ticket_log
WHERE is_winner=1
GROUP BY ticket_id
ORDER BY wins DESC;
这是吗?
答案 1 :(得分: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
(编辑因为我注意到你需要所有获奖者)