首頁  >  文章  >  資料庫  >  常用的sql語句

常用的sql語句

巴扎黑
巴扎黑原創
2017-06-23 15:01:311106瀏覽

1.查詢每位使用者最新的發言記錄:

select max(time) from 2017sxgf group by id order by time desc limit 10;

 

2.找到發言數最多的使用者ID和次數

select userid,count(userid) from orders  where userid != '' group by userid order by count(userid) desc limit 1;

3.關於MySQL中每個使用者取1筆記錄的三種寫法

第一種是先排序,然後group,這樣的話自然可以取到最適合的一條數據。
缺點很明顯:Using temporary; Using filesort

select * from (select * from 2017sxgf order by time desc)t group by mobile limit 10;

 

#第二種是聯合查詢 

select * from (select max(time) as btime  from 2017sxgf group by mobile limit 10)t left join  2017sxgf as s on t.btime = s.time;

 

#第三種是子查詢

##select * from 2017sxgf where exists(select mobile from (select max(time) as btime from 2017sxgf  group by mobile limit 10)t where t.btime = 2017sxgf.time);

 

##5.

#C

################ ###

以上是常用的sql語句的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn