第一个查询
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/