# 集合A1 mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where not exists(select * from t1 where t1.id=t3.id and t1.id=3); +-----+------+----------+------+ | sid | id | name | eid | +-----+------+----------+------+ | 1 | 1 | superman | 1 | | 2 | 1 | superman | 2 | | 3 | 2 | batman | 3 | +-----+------+----------+------+ # 集合A2 select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where not exists(select * from t1 where t1.id=t3.id and t1.id=1); +-----+------+--------+------+ | sid | id | name | eid | +-----+------+--------+------+ | 3 | 2 | batman | 3 | +-----+------+--------+------+ # 集合B mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select * from t2 where t2.eid=t3.eid and t2.ename='mysql') or exists(select * from t1 where t1.id=t3.id and t1.id=1) or exists(select * from t1 where t1.id=t3.id and t1.id=2)); +-----+------+----------+------+ | sid | id | name | eid | +-----+------+----------+------+ | 1 | 1 | superman | 1 | | 2 | 1 | superman | 2 | | 3 | 2 | batman | 3 | +-----+------+----------+------+ ============================ # 集合A1和集合B的交集 mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select * from t2 where t2.eid=t3.eid and t2.ename='mysql') or exists(select * from t1 where t1.id=t3.id and t1.id=1) or exists(select * from t1 where t1.id=t3.id and t1.id=2)) and not exists(select * from t1 where t1.id=t3.id and t1.id=3); +-----+------+----------+------+ | sid | id | name | eid | +-----+------+----------+------+ | 1 | 1 | superman | 1 | | 2 | 1 | superman | 2 | | 3 | 2 | batman | 3 | +-----+------+----------+------+
# 集合A2和集合B的交集 mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select * from t2 where t2.eid=t3.eid and t2.ename='mysql') or exists(select * from t1 where t1.id=t3.id and t1.id=1) or exists(select * from t1 where t1.id=t3.id and t1.id=2)) and not exists(select * from t1 where t1.id=t3.id and t1.id=1); +-----+------+--------+------+ | sid | id | name | eid | +-----+------+--------+------+ | 3 | 2 | batman | 3 | +-----+------+--------+------+ ============================================ # 如果 集合B中a,b,c的列不同呢? mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select t2.eid from t2 where t2.eid=t3.eid and t2.ename='mysql') or exists(select t1.name from t1 where t1.id=t3.id and t1.id=1) or exists(select t1.id from t1 where t1.id=t3.id and t1.id=2)); +-----+------+----------+------+ | sid | id | name | eid | +-----+------+----------+------+ | 1 | 1 | superman | 1 | | 2 | 1 | superman | 2 | | 3 | 2 | batman | 3 | +-----+------+----------+------+
select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select t2.eid from t2 where t2.eid=t3.eid and t2.ename='mysql') ) union select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select t1.name from t1 where t1.id=t3.id and t1.id=1) ) union select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select t1.id from t1 where t1.id=t3.id and t1.id=2) );
mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select t2.eid from t2 where t2.eid=t3.eid and t2.ename='mysql') ); +-----+------+----------+------+ | sid | id | name | eid | +-----+------+----------+------+ | 1 | 1 | superman | 1 | +-----+------+----------+------+ 1 row in set (0.00 sec)
mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select 1 from t2 where t2.eid=t3.eid and t2.ename='mysql') ); +-----+------+----------+------+ | sid | id | name | eid | +-----+------+----------+------+ | 1 | 1 | superman | 1 | +-----+------+----------+------+
# 我们发现子查询中select的列不管是写什么都没有影响,永远都是一个包含所有列的结果集 mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select t1.name from t1 where t1.id=t3.id and t1.id=1) ); +-----+------+----------+------+ | sid | id | name | eid | +-----+------+----------+------+ | 1 | 1 | superman | 1 | | 2 | 1 | superman | 2 | +-----+------+----------+------+ 2 rows in set (0.00 sec)
mysql> select sid,t3.id,t1.name,t3.eid from t3 join t1 on t1.id=t3.id where (exists(select t1.name from t1 where t1.id=t3.id and t1.id=1 limit 1) ); +-----+------+----------+------+ | sid | id | name | eid | +-----+------+----------+------+ | 1 | 1 | superman | 1 | | 2 | 1 | superman | 2 | +-----+------+----------+------+
|