如何从表中选择不在select Query结果中的记录(select query可能是2个或更多表的连接)

时间:2015-11-03 12:52:33

标签: mysql sql-server sql-server-2008

考虑一下:

table t1(a,b,c)
table t2(a,d,e)
table t3(r,f,g)

select t1.a,t1.b,t1.c,t2.d,t2.e
from t1 join t2
on (t1.a=t2.a)

还需要根据t1.a和t3.r选择t3表中不在上述查询结果中的记录

3 个答案:

答案 0 :(得分:0)

如果t1.at3.r之间存在关联,那么只需在表之间建立联接,例如

select t1.a,
       t1.b,
       t1.c,
       t2.d,
       t2.e,
       t3.g
from t1 
join t2 on t1.a = t2.a
join t3 on t1.a = t3.r;

答案 1 :(得分:0)

如果t1.at3.r之间没有关系,那么:

SELECT t1,*, t2.*, t3.*
FROM t1
JOIN t2 ON t1.a = t2.a
JOIN t3 ON 1=1

答案 2 :(得分:0)

NOT IN子句中尝试WHERE,方法如下:

SELECT
    t3.*
FROM
    t3 AS t3
WHERE
    t3.r NOT IN
        (select t1.a
         from t1 join t2
         on (t1.a=t2.a))