当前位置: 代码迷 >> Sql Server >> 求SQL 语句.
  详细解决方案

求SQL 语句.

热度:37   发布时间:2016-04-24 19:23:34.0
求SQL 语句.在线等.
表A  
id  name remark
 1  张三1  备注1
 2  张三2  备注2
 3  张三3  备注3
 4  张三4  备注4
 5  张三5  备注5

表B 
 Id  Aid   状态1   状态2 
 1    1    true    false
 2    1    false   false
 3    1    false   false
 4    2    true    false
 5    2    true    false 
 6    2    true    true 
 7    3    false   true 
 8    3    false   true
 9    4    false   false
10    5    true    true

我想查询出 表B中状态1为false,状态2为true  注意是全部. (B表数据不固定)
想得到的结果是:
 3  张三3  备注3

------解决方案--------------------
引用:
Quote: 引用:


--构造数据
select 1 id ,'张三1' name,'备注1' remark
into #A
union all select 2  ,'张三2','备注2'
union all select 3  ,'张三3','备注3'
union all select 4  ,'张三4','备注4'
union all select 5  ,'张三5','备注5'

 select  1 Id , 1 Aid,'true ' 状态1,'false' 状态2
 into #B
union all select  2 , 1,'false','false'
union all select  3 , 1,'false','false'
union all select  4 , 2,'true ','false'
union all select  5 , 2,'true ','false'
union all select  6 , 2,'true ','true '
union all select  7 , 3,'false','true '
union all select  8 , 3,'false','true '
union all select  9 , 4,'false','false'
union all select 10 , 5,'true ','true '

--查询
select * 
from #A
where id in
(
select aid
from #B
where [状态1]='false' and [状态2]='true'
)



我数据弄错了.不好意思. 如果按照你这么查询 如果存在这样的数据也会被查出来
表B 
 1 , 5,true ,true 
 2 , 5,false,true 
 3 , 5,false,true 




--构造数据
select 1 id ,'张三1' name,'备注1' remark
into #A
union all select 2  ,'张三2','备注2'
union all select 3  ,'张三3','备注3'
union all select 4  ,'张三4','备注4'
union all select 5  ,'张三5','备注5'

 select  1 Id , 1 Aid,'true ' 状态1,'false' 状态2
 into #B
union all select  2 , 1,'false','false'
union all select  3 , 1,'false','false'
union all select  4 , 2,'true ','false'
union all select  5 , 2,'true ','false'
union all select  6 , 2,'true ','true '
union all select  7 , 3,'false','true '
union all select  8 , 3,'false','true '
union all select  9 , 4,'false','false'
union all select 10 , 5,'true ','true '

--查询
select * 
from #A
where id in
(
select aid
from
(
select aid,sum(case when [状态1]='false' and [状态2]='true' then 1 end ) ftc
,count(1) rc
from #B
group by aid
) t
where ftc=rc
)

改成这样
------解决方案--------------------
是这样吗:
create table 表A(id  int,name varchar(20), remark varchar(20))  

insert into 表A
  相关解决方案