gpt4 book ai didi

mysql - 删除一个表中未被另一个表引用的所有行

转载 作者:行者123 更新时间:2023-11-29 01:16:55 25 4
gpt4 key购买 nike

我有两个表:

CREATE TABLE  "status" (
"id" integer NOT NULL PRIMARY KEY,
"created_at" datetime NOT NULL,
"updated_at" datetime NOT NULL);


CREATE TABLE "device" (
"id" integer NOT NULL PRIMARY KEY,
"created_at" datetime NOT NULL,
"updated_at" datetime NOT NULL,
"last_status_object_id" integer REFERENCES "status" ("id"));

在“device”表中,last_status_object_id 引用了 status.id。

我想删除所有未被“last_status_object_id”引用的状态行。我似乎无法弄清楚如何实现这一目标。谁能帮帮我?

最佳答案

DELETE
status
FROM
status
LEFT JOIN device ON (status.id = last_status_object_id)
WHERE
device.id IS NULL

关于mysql - 删除一个表中未被另一个表引用的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19545704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com