求SQL语句

Tosp2012 2011-04-24 06:02:11
A表
Rm_code,P_no
1001 A101
1001 B102
1002 A101
1002 B102

B 表

Rm_Code ,P_No
1001 B102
1002 A101


求 A表存在记录,而B表不存在的记录
结果为:
Rm_Code,P_No
1001 A101
1002 B102
...全文
105 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mabailin 2011-04-24
  • 打赏
  • 举报
回复

select a.* from a left join b
on a.rm_code=b.rm_code and a.P_no=b.P_no
where b.Rm_code is null
oO寒枫Oo 2011-04-24
  • 打赏
  • 举报
回复

create table A
(
Rm_code int not null,
P_no nvarchar(10) not null
)
insert into A(Rm_code,P_no) values(1001,'A101')
insert into A(Rm_code,P_no) values(1001,'B102')
insert into A(Rm_code,P_no) values(1002,'A101')
insert into A(Rm_code,P_no) values(1002,'B102')

create table B
(
Rm_code int not null,
P_no nvarchar(10) not null
)
insert into B(Rm_code,P_no) values (1001,'B102')
insert into B(Rm_code,P_no) values(1002,'A101')


select * from A except select * from B
Tosp2012 2011-04-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lf19820717 的回复:]
SQL code

use tempdb;
/*
create table A
(
Rm_code int not null,
P_no nvarchar(10) not null
);
insert into A(Rm_code,P_no)
values
(1001,'A101'),
(1001,'B102'),
(1002,'A101'),
(100……
[/Quote]

Incorrect syntax near the keyword 'except'.
Tosp2012 2011-04-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ssp2009 的回复:]
SQL code
select * from 表A
except
select * from 表B
[/Quote]
Incorrect syntax near the keyword 'except'.
javatemptation 2011-04-24
  • 打赏
  • 举报
回复

select * from A
where not exists
(select * from B where A.Rm_code = B.Rm_code and A.P_no = B.P_no);


在A中查找不满足如下情况的内容,什么情况呢?
A的两个字段的值分别等于B的两个字段的值。
javatemptation 2011-04-24
  • 打赏
  • 举报
回复

use tempdb;
/*
create table A
(
Rm_code int not null,
P_no nvarchar(10) not null
);
insert into A(Rm_code,P_no)
values
(1001,'A101'),
(1001,'B102'),
(1002,'A101'),
(1002,'B102');

create table B
(
Rm_code int not null,
P_no nvarchar(10) not null
);
insert into B(Rm_code,P_no)
values
(1001,'B102'),
(1002,'A101');
*/
--求 A表存在记录,而B表不存在的记录
select * from A
except
select * from B;
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 roy_88 的回复:]
SQL code
select * from a where not exists(select 1 from b where Rm_Code=a.Rm_Code and P_No
=a.P_No
)
[/Quote]

select 1 from b 1什么意思
快溜 2011-04-24
  • 打赏
  • 举报
回复
select * from 表A
except
select * from 表B
中国风 2011-04-24
  • 打赏
  • 举报
回复
select * from a where not exists(select 1 from b where Rm_Code=a.Rm_Code and P_No
=a.P_No
)

34,575

社区成员

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

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