gpt4 book ai didi

mysql - 将选择和删除查询作为单个语句

转载 作者:行者123 更新时间:2023-11-29 01:58:47 26 4
gpt4 key购买 nike

第一次查询

    select id from posts where post_title='abc' || post_title='xyz' order by id desc limit 1;

假设返回值为 730 和 735。

下一个查询

    delete from posts where id in(730,735);

我希望将这两个查询合并为一个语句。如何做呢。请帮忙

我在下面试过这个。它不起作用。

delete from posts where id in
(
select id from posts where post_title='abc' order by id desc limit 1,
select id from posts where post_title='xyz' order by id desc limit 1
);

最佳答案

在我看来,在一个查询中执行 delete-and-select 时,我们必须使用 IF-EXISTS 子句,因为如果 select 返回 null,它会抛出异常,所以试试这个:

IF EXISTS (SELECT id FROM [Posts] WHERE post_title IN ('abc', 'xyz'))
BEGIN
DELETE FROM posts
WHERE id IN (SELECT id
FROM [Posts]
WHERE post_title IN ('abc', 'xyz')
ORDER BY post_title, id DESC
)
END

关于mysql - 将选择和删除查询作为单个语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20783264/

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