1. 程式人生 > >SQL語句查詢重複欄位並按數量排序

SQL語句查詢重複欄位並按數量排序

SQL語句

描述:exhibitiononline表主鍵exhiid,    exhibitor表有外來鍵 exhid,對應表 exhibitiononline的主鍵exhiid;

          exhibitiononline表--------->           一對多                <------------- exhibitor表

功能:查詢出 exhibitiononline表中,exhiid存在於表 exhibitor中的所有資料,並根據  exhibitor表的exhid的數量排序

select * from exhibitiononline  join 
(select exhid,count(exhid) as s from exhibitor group by exhid) as t
on exhibitiononline.exhiid=t.exhid
order by t.s desc