两张表 a b
a 表中有两个字段 a.1 a.2 和b表中两个字段 b.3 b.4
两个表没有关联关系
我想查出 在a表中 a.1=b.3 并且 a.2 <> b.4 数据
select *
from a b
where a.1 = b.3 and a.2<>b.4 ;
并不能得到想要的结果。
a 表中有两个字段 a.1 a.2 和b表中两个字段 b.3 b.4
两个表没有关联关系
我想查出 在a表中 a.1=b.3 并且 a.2 <> b.4 数据
select *
from a b
where a.1 = b.3 and a.2<>b.4 ;
并不能得到想要的结果。
FROM a
WHERE EXISTS (SELECT 1
FROM b
WHERE WHERE a.1 = b.3
AND a.2 <> b.4)
select * from t21 1 10
2 1 20
3 1 40SELECT * FROM t3;1 1 10
2 1 20
3 1 30
4 2 10
5 2 20
6 2 30
select * from t2
where exists(select 1 from t3 where t2.id=t3.id)
and not exists (select 1 from t3 where t2.m1=t3.m2)
SELECT * FROM t2,t3
where t2.id=t3.id
and t2.m1<>t3.m2
这两个的区别: 上边是显示你说的意思? 下边是 符合这种条件的t2 中的记录与t3 的全部组合。
SELECT * FROM t2
minus
SELECT t2.* FROM t2,t3
where t2.id=t3.id
and t2.m1=t3.m2<>SELECT t2.* FROM t2,t3
where t2.id=t3.id
and t2.m1<>t3.m2
我也理不清楚了,一个是集合的概念,是一个关联关联关系的概念,求大师详解
where t2.id=t3.id
and t2.m1=t3.m2SELECT t2.* FROM t2,t3
where t2.id=t3.id
and t2.m1<>t3.m2这两个查询集合合并起来是
SELECT t2.* FROM t2,t3
where t2.id=t3.id
这个集合按你给的数据与SELECT * FROM t2肯定是不一样的,至于为啥,你应该能想清楚
select * from a b where a.1 = b.3
再从新表里对于大的值进行筛选
select * from (select * from a b where a.1 = b.3) where a.2<>b.4 ;
inner join b on a.1=b.3
where a.2<>b.4
也可以用内连接SELECT * FROM A INNER JOIN B WHERE ON A.1=B.3 AND A.2<>B.4