[英]How to select all the rows having same and latest value in a particular column from database mysql
您可以使用sunquery最長時間
select * from my_table
where time1 = (
select max(time1)
from my_table
)
這是一個依賴於窗口函數RANK()
的解決方案,該函數自MySQL 8.0起可用:
SELECT *
FROM (SELECT id, name, time1, RANK() OVER(ORDER BY time1 DESC) rnk FROM mytable) x
WHERE rnk = 1
內部查詢為每條記錄分配一個等級,按降序排序; 最高的領帶排名相同, 1
。 然后,外部查詢僅過濾具有等級1
記錄。
請嘗試一下
select * from table1 where time1 =(select time1 from table1 order by time1 limit 0,1);
要么
select * from table1 where time1 = ( select max(time1) from table1 )
要么
select * from table1 where time1=(select TOP 1 time1 from table1 order by time1 desc )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.