数据库
首页 > 数据库> > MySQL在IN条件下使用UNION子查询删除

MySQL在IN条件下使用UNION子查询删除

作者:互联网

我遇到了一个奇怪的SQL错误.最后一个查询不起作用.当然,我可以将DELETE分为三个查询,但是我真的很奇怪,为什么MySQL不允许我这样做.

一个小例子:

(SELECT id FROM stairs WHERE building = 123)
UNION
(SELECT id FROM lift WHERE building = 123)
UNION
(SELECT id FROM qrcodeid WHERE building = 123)

作品!

DELETE FROM startpoint WHERE id IN (SELECT id FROM stairs WHERE building = 123)

也可以!

DELETE FROM startpoint WHERE id IN (
    (SELECT id FROM stairs WHERE building = 123)
    UNION
    (SELECT id FROM lift WHERE building = 123)
    UNION
    (SELECT id FROM qrcodeid WHERE building = 123)
)

引发错误

#1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘UNION (SELECT id FROM lift WHERE building = 123) UNION (SELECT id FROM qrc’ at line 3

任何线索?

解决方法:

试试那个SQL

DELETE FROM startpoint WHERE id IN (
SELECT id FROM stairs WHERE building = 123
UNION
SELECT id FROM lift WHERE building = 123
UNION
SELECT id FROM qrcodeid WHERE building = 123)

标签:mysql,sql-delete,subquery,union,in-subquery
来源: https://codeday.me/bug/20191011/1894427.html