关于多张表求交集的sql语句,急急急!

linhua51 2012-11-07 08:44:56
有2表
A表
ID 结果
001 10
002 12
003 12
B表
ID 结果
001 80
002 90
003 70
现在想等到即在a表中结果大于10的,又在b表中结果大于80的id,得到的id002,求这样的sql语句。
...全文
381 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luxiangrui_91 2012-11-09
  • 打赏
  • 举报
回复
楼主 发表于: 2012-11-07 08:44:56 本帖最后由 linhua51 于 2012-11-07 08:46:31 编辑 有2表 A表 ID 结果 001 10 002 12 003 12 B表 ID 结果 001 80 002 90 003 70 select * from a,b where a.id=b.id and a.结果>10 and b.结果
蓝色羽翼 2012-11-07
  • 打赏
  • 举报
回复
select ID from a where id in (select * from b where 结果>80) and 结果>10
坚_持 2012-11-07
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :TravyLee(物是人非事事休,欲语泪先流!)
-- Date    :2012-11-07 09:05:30
-- Version:
--      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
--	Jul  9 2008 14:43:34 
--	Copyright (c) 1988-2008 Microsoft Corporation
--	Developer Edition on Windows NT 6.1 <X86> (Build 7600: )
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go 
create table [a]([ID] varchar(3),[结果] int)
insert [a]
select '001',10 union all
select '002',12
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go 
create table [b]([ID] varchar(3),[结果] int)
insert [b]
select '001',80 union all
select '002',90 union all
select '003',90
--> 测试数据:[c]
if object_id('[c]') is not null drop table [c]
go 
create table [c]([ID] varchar(3),[结果] int)
insert [c]
select '002',50 union all
select '003',60
--> 测试数据:[d]
if object_id('[d]') is not null drop table [d]
go 
create table [d]([ID] varchar(3),[结果] int)
insert [d]
select '002',50 union all
select '003',60 union all
select '004',60
go
--a表中结果大于10的,
--又在b表中结果大于80的id,
--又在c表中结果大于40,
--又在d表中结果大于等于50
select
	[ID]
from
	a
where 
	exists(select 1 from b where a.ID=b.ID and b.结果>=80)
	and exists(select 1 from c where a.ID=c.ID and c.结果>40)
	and exists(select 1 from d where a.ID=d.ID and d.结果>=50)
	and a.结果>=10
----------------结果----------------------------
/* 
ID
----
002

(1 行受影响)



*/
linhua51 2012-11-07
  • 打赏
  • 举报
回复
有2表 A表 ID 结果 001 10 002 12 B表 ID 结果 001 80 002 90 003 90 c表 ID 结果 002 50 003 60 d表 ID 结果 002 50 003 60 004 60 现在想等到即在a表中结果大于10的,又在b表中结果大于80的id,又在c表中结果大于40,又在d表中结果大于等于50。
linhua51 2012-11-07
  • 打赏
  • 举报
回复
引用 1 楼 ssp2009 的回复:
select * from a, b where a.id=b.id and a.结果=10 and b.结果>80
a表中记录跟b表的记录不一样,如果还有c表,d表等多表关联,这样写会不会很复杂。
快溜 2012-11-07
  • 打赏
  • 举报
回复
select * from a, b where a.id=b.id and a.结果=10 and b.结果>80
开启时代 2012-11-07
  • 打赏
  • 举报
回复
select ID from A where 结果>10
intersect
select ID from B where 结果>80
intersect
select ID from C where 结果>40
intersect
select ID from d where 结果>=50
dd_2012 2012-11-07
  • 打赏
  • 举报
回复

select a.id 
from a, b,c,d 
where a.id=b.id and b.id=c.id and c.id=d.id 
and a.结果>10 and b.结果>80 and c.结果>40 and d.结果>=50

34,590

社区成员

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

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