我想將最近洗掉的日期作為一個訂單項。以下查詢列出了所有名稱。但是,在輸出中,我只想要一個顯示具有最近洗掉日期的名稱的行專案。我該怎么做呢?
select table1.name, table1.removed_date
from table1
where table1.status = 'Removed' and table1.removed_date is not null
order by table1.removed_date desc
uj5u.com熱心網友回復:
只需limit 1在查詢末尾添加即可。是的,這就是答案。
uj5u.com熱心網友回復:
這可能有點極端,但經過仔細考慮,我認為即使以毫秒為精度,兩個時間戳仍有可能發生沖突。因此,要定義最近的記錄,我們可以利用 MySQL 的最新合法事務。因此,記錄最新 UPDATE 的表可能非常有用。從根本上說,該表只需要一行一列記錄table1的ID(假設OP的table1確實有一個以避免重復名稱)。然后一個 UPDATE 觸發器可以完成剩下的作業。這是我在作業臺上撰寫和測驗的代碼:
delimiter //
CREATE TABLE latest_removal (id int(11) )//
insert latest_removal values(null)//
create trigger latest_removed before update on table1 for each row begin
if old.status != new.status and new.status='removed' then
update latest_removal set id=old.id ;
set new.removed_date=current_date(); -- This is optional as people may prefer manual updating.
end if;
end //
如果您覺得這有幫助,請發表評論。謝謝你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453108.html
下一篇:如何替換字串的一部分
