简体   繁体   English

MySQL-过滤子查询中的联合

[英]MySQL - Union in a filtering subquery

I'm able to run the following: 我可以执行以下操作:

SELECT * FROM t1 WHERE t1.id NOT IN (SELECT id FROM t2);

As well as: 以及:

SELECT * FROM (SELECT id FROM t2 UNION SELECT id FROM t3);

But MySQL complains when I attempt to run the following: 但是当我尝试运行以下命令时,MySQL会抱怨:

SELECT * FROM t1 WHERE t1.id NOT IN (SELECT id FROM t2 UNION SELECT id FROM t3);

The error is "unexpected 'SELECT'" for the third select. 对于第三个选择,错误为“意外的'SELECT'”。 Is something wrong with my syntax? 我的语法有问题吗? Is this not possible? 这不可能吗? I'm open to rewriting using EXISTS if that's the only way. 如果那是唯一的方法,我愿意使用EXISTS进行重写。

They are not mistakes on your query: 它们不是您查询中的错误:

SQL Fiddle SQL小提琴

MySQL 5.6 Schema Setup : MySQL 5.6模式设置

create table t1 ( id int );
create table t2 ( id int );
create table t3 ( id int );

insert into t1 values (1);
insert into t2 values (2);
insert into t3 values (3);

Query 1 : 查询1

SELECT * FROM t1 WHERE t1.id NOT IN
(SELECT id FROM t2 UNION SELECT id FROM t3)

Results : 结果

| id |
|----|
|  1 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM