gpt4 book ai didi

mysql - 使用另一个表中的 where 子句从表中删除行

转载 作者:行者123 更新时间:2023-11-29 18:46:15 24 4
gpt4 key购买 nike

当我思考如何做到这一点时,我已经伤透了脑筋。需要进行查询,条件是与当前表项的id进行比较。

也就是说,类似于:

DELETE
FROM table1
WHERE TIMESTAMPDIFF(MINUTE, Time_ADD, now()) >= (
SELECT del_time
FROM table2
WHERE table1.id = table2.id
)

类似的查询会产生错误,表明如果未为 id 指定 table1,则子查询会发出多个值,或者如果指定了 table1 名称,则 table1.id 不存在

附注如果我犯了错误,请原谅我的英语。

编辑 1对于前。 CREATE 查询可能看起来像:

CREATE TABLE table1(
id int identity(1,1),
some_value varchar(max),
Time_ADD datetime
)

CREATE TABLE table2(
id int,
del_time int
)

在表2中,我有以分钟为单位的整数值。此后 id int table1 必须被删除。

最佳答案

这可能就是您要找的

DELETE table1
FROM table1
INNER JOIN table2 ON table1.ID = table2.ID
AND TIMESTAMPDIFF(MINUTE, Time_ADD, now()) >= table2.del_time

关于mysql - 使用另一个表中的 where 子句从表中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44623956/

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