1.查詢每個(gè)用戶最新的發(fā)言記錄:
select max(time) from 2017sxgf group by id order by time desc limit 10;
?
2.找到發(fā)言數(shù)最多的用戶ID和次數(shù)
select userid,count(userid) from orders ?where userid != ” group by userid order by count(userid) desc ?limit 1;
3.關(guān)于MySQL中每個(gè)用戶取1條記錄的三種寫法
第一種是先排序,然后group,這樣的話自然可以取到最適合的一條數(shù)據(jù)。
缺點(diǎn)很明顯:Using temporary; Using filesort
select * from (select * from 2017sxgf order by time desc)t group by mobile limit 10;
?
第二種是聯(lián)合查詢?
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.
?
?