两表关联查询疑问?

weizewang 2010-10-10 02:34:05
table1如下:
id
title
txt


table2如下
id
iid


需要查询的是:table2里面的iid不包含table1 id的所有记录.
...全文
46 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weizewang 2010-10-10
  • 打赏
  • 举报
回复
谢谢大家!问题已解决!
饮水需思源 2010-10-10
  • 打赏
  • 举报
回复
select * from table2 where iid not in(select id from table1)
叶子 2010-10-10
  • 打赏
  • 举报
回复

declare @table1 table (id int,title varchar(1),txt varchar(1))
insert into @table1
select 1,'a','d' union all
select 2,'b','e' union all
select 3,'c','f'

declare @table2 table (id int,iid int)
insert into @table2
select 1,2 union all
select 2,3 union all
select 3,4

select b.* from @table2 b
left join @table1 a
on b.iid=a.id
where a.id is null
/*
id iid
----------- -----------
3 4
*/
叶子 2010-10-10
  • 打赏
  • 举报
回复

select * from table2 a
left join table1 b on a.iid=b.id
where b.id is null
SQLCenter 2010-10-10
  • 打赏
  • 举报
回复
select * from table2 t2 where not exists (select 1 from table1 where id=t2.iid)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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