简单问题。。

wxylvmnn 2014-12-10 08:35:38
有数据如下:
declare @t1 table(isKey varchar(4),isFields varchar(10),isData varchar(10))
insert @t1
select '0001','A','001' union all
select '0001','B','002' union all
select '0001','C','003' union all
select '0001','D','001' union all

select '0002','C','001' union all
select '0002','F','002' union all
select '0002','D','001' union all
select '0002','B','002'

问题:
我该如何取出,
同时满足 isFields='A' and isData='001' 并且 isFields='B' and isData='002' 的数据,所对应的isKey的值?
※按照上面的测试数据的话,得出'0001'
...全文
155 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhl8198 2014-12-10
  • 打赏
  • 举报
回复
select a.isKey from (select * from @t1 where isFields='A' and isData='001')a join (select * from @t1 where isFields='B' and isData='002')b on a.isKey=b.isKey
hepe00 2014-12-10
  • 打赏
  • 举报
回复

declare @t1 table(isKey varchar(4),isFields varchar(10),isData varchar(10))
insert @t1 
select '0001','A','001' union all
select '0001','B','002' union all
select '0001','C','003' union all
select '0001','D','001' union all

select '0002','C','001' union all
select '0002','F','002' union all
select '0002','D','001' union all
select '0002','B','002'

-----------------------------------------
--逻辑:1、a链接b,iskey一定相等
--		2、a的条件,并且b的条件
-----------------------------------------
select a.isKey from @t1 as a
	join @t1 b on a.isKey=b.isKey	--isKey相等
	where (a.isFields='A' and a.isData='001')   --a条件
		and (b.isFields='B' and b.isData='002' ) --b条件

---涛声依旧--- 2014-12-10
  • 打赏
  • 举报
回复

SELECT DISTINCT t.isKey FROM @t1 t
WHERE EXISTS(SELECT 1 FROM @t1 WHERE isKey=t.isKey AND isFields='A' AND isData='001')
AND EXISTS(SELECT 1 FROM @t1 WHERE isKey=t.isKey AND isFields='B' AND isData='002')
xdashewan 2014-12-10
  • 打赏
  • 举报
回复

select a.* from @t1 a
where a.isFields='A' and a.isData='001' 
inner join
(select isKey from @t1
where isFields='B' and isData='002' ) b on a.isKey = b.isKey

34,591

社区成员

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

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