[英]How can I select all parent rows with a foreign key child row older than a certain date?
我有一個訂單表和一個更新表,它們通過一對多(更新)關系與訂單連接。 我正在嘗試選擇過去24小時內未更新的所有訂單。
這是updates
表(不在我的頭上):
id INT PRIMARY KEY AUTO INCREMENT,
order_id INT NOT NULL ,
update VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
foreign key (order_id) REFERENCES orders(id)
和orders
表:
id INT PRIMARY KEY AUTO INCREMENT,
enabled TINYINT(1) NOT NULL DEFAULT 0,
reference VARCHAR(10) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
如何退回所有已啟用的訂單,這些訂單尚未更新或最新更新早於一天? (對我來說,可以使用純MySQL,或者最好使用Laravel的Eloquent ORM。)
謝謝。
我會執行NOT EXISTS
,以返回沒有最新更新的訂單:
select * from orders o
where enabled = 1
and not exists (select 1 from updates u
where u.order_id = o.id
and u.created_at >= NOW() - INTERVAL 1 DAY)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.