菜鸟初学问题,如何找出两个表中字段A不等于字段B的记录!及时揭帖

nirvana_boy 2003-09-13 03:31:14
两个表1和2,分别有两个字段A和B
我想找出表1中的A不等于表2中的B的记录!
我用
SELECT a,b from 1,2
where 1.a=2.b
可以找出相等的记录
例如1和2各有10条记录,我用了这个过后查出有8各记录
那么另外两个记录该怎么找呢

如果要用
SELECT a,b from 1,2
where 1.a!=2.b的话却不能找出来,列出来的就是一大串了

该怎么写才好呢
...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nirvana_boy 2003-09-13
  • 打赏
  • 举报
回复
非常感谢

你都帮我好多次了,你的热情着实让我………………

谢谢!

我区试试
yujohny 2003-09-13
  • 打赏
  • 举报
回复
create Table 表1(ID int,a int)
create table 表2(ID int,b int)
insert into 表1 values(1,1)
insert into 表1 values(2,3)
insert into 表1 values(3,5)
insert into 表1 values(4,9)
insert into 表1 values(5,7)

insert into 表2 values(1,4)
insert into 表2 values(2,6)


select a from 表1 where a not in (select b from 表2)
union
select b from 表2 where b not in (select a from 表1)

drop table 表1
drop table 表2
测试结果:
1
3
4
5
6
7
9
wzh1215 2003-09-13
  • 打赏
  • 举报
回复
select a,b from 1,2 where 1.a<>2.b
yujohny 2003-09-13
  • 打赏
  • 举报
回复
select a from 1 where a not in (select b from 2)
union
select b from 2 where b not in (select a from 1)
nirvana_boy 2003-09-13
  • 打赏
  • 举报
回复
就是没关联字段啊
yujohny 2003-09-13
  • 打赏
  • 举报
回复
两个表的关联字段是什么????
yujohny 2003-09-13
  • 打赏
  • 举报
回复
select 1.a,2.b from 1,2 where 1.a<>2.B

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧