如果查询另一个表没有对应的

bright0826 2008-05-05 09:52:25
Table1保存产品列表
Table2保存产品的属性
相关联的字段为ID
现在要找出没有属性的产品。
...全文
57 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fwacky 2008-05-05
  • 打赏
  • 举报
回复

select * from Table1 where id not in (select id from Table2)

rockyvan 2008-05-05
  • 打赏
  • 举报
回复

--子查詢
Select Distinct Table1.ID
From Table1
Where Table1.ID Not In (Select Distinct Table2.ID From Table2)
--左聯接
Select Distinct Table1.ID
From Table1 Left Join Table2 On Table1.ID=Table2.ID
Where Table2.ID Is NULL
Limpire 2008-05-05
  • 打赏
  • 举报
回复
select * from Table1 where not exists (select 1 from Table2 where Table1.ID=Table2.ID)
-狙击手- 2008-05-05
  • 打赏
  • 举报
回复
select a.*
from table1 a
where not exists(select 1 from table2 where id = a.id)
fuanwei 2008-05-05
  • 打赏
  • 举报
回复
select * from (
select Table1.*,Table1.ID as IDD from Table1
left join Table2 on Table2.ID=Table1.ID) as a
where a.IDD is null
liangCK 2008-05-05
  • 打赏
  • 举报
回复
select * from table1
where id not in(select 产品ID from table2)
fuanwei 2008-05-05
  • 打赏
  • 举报
回复
select * from (
select Table1.*,Table1.ID as IDD from Table1
left join Table2 on Table2.ID=Table1.ID) as a
where a.IDD is not null

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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