mysql查詢表中日期最大的那條資料

指尖聽音發表於2019-12-23

資料庫中有這樣的一張表,現在要查詢日期最大的那條資料。

file
直接寫sql語句如下:

select name,max(gmt_create) from user
複製程式碼

得到結果:

file
但是這樣輸出結果並不正確,name的值不對。 修改sql語句如下:

select a.name,max(a.gmt_create) from user a,
(select name,max(gmt_create) max_day from user ) b 
where a.gmt_create=b.max_day
複製程式碼

得到結果:

file
顯然,此時輸出結果正確

相關文章