[英]Return the values of max count of rows with the same values
我有table_schedule(id,instructorNumber,time,date,cpNumber,user_id);
是否可以輸出具有相同最高出現次數和相同值的講師編號,時間等值?
抱歉,我是sql的新手,所以我希望有人可以幫助我進行查詢
只需對“相同值比較”所需的所有字段進行分組,然后按desc計數(因此,出現次數最多的結果將排在第一位),然后排在第一位。
select
instructorNumber, time, date, cpNumber
from table_schedule
group by instructorNumber, time, date, cpNumber
order by count(*) desc
LIMIT 1
如果您想要多個結果,則可以將其用作主查詢的聯接。
首先對要比較和計數的值進行分組。 獲取最大數量(在MySQL中,您可以使用LIMIT)。 然后再次進行相同的分組查詢,並僅獲取具有最大計數的結果。 使用GROUP_CONCAT獲取字符串中的ID列表。
select instructorNumber, time, date, cpNumber, user_id, group_concat(id) as ids
from table_schedule
group by instructorNumber, time, date, cpNumber, user_id
having count(*) =
(
select count(*)
from table_schedule
group by instructorNumber, time, date, cpNumber, user_id
order by count(*) desc limit 1
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.