SQL语句不会写 求大神帮忙.
SQL语句不会写 求大神帮忙.在线等.....
表A 表B
Id Name Remark Id AId State
1 张山 无 1 1 true
2 王二 无 2 1 false
3 李四 无 3 1 false
4 2 true
5 2 true
6 3 false
7 3 true
我想查询 表B 状态全部为true 的表A数据
结果: ID Name Remark
2 王二 无
------解决方案--------------------
------解决方案--------------------
表A 表B
Id Name Remark Id AId State
1 张山 无 1 1 true
2 王二 无 2 1 false
3 李四 无 3 1 false
4 2 true
5 2 true
6 3 false
7 3 true
我想查询 表B 状态全部为true 的表A数据
结果: ID Name Remark
2 王二 无
------解决方案--------------------
create table 表A(Id int, Name varchar(10), Remark varchar(10))
insert into 表A
select 1 ,'张山' ,'无' union all
select 2 ,'王二' ,'无' union all
select 3 ,'李四' ,'无'
create table 表B(Id int, AId int, State varchar(10))
insert into 表B
select 1 ,1 ,'true' union all
select 2 ,1 ,'false' union all
select 3 ,1 ,'false' union all
select 4 ,2 ,'true' union all
select 5 ,2 ,'true' union all
select 6 ,3 ,'false' union all
select 7 ,3 ,'true'
select *
from 表A
where ID in (select aid from 表B
group by aid
having COUNT(*)=COUNT(case when State = 'true ' then 1 else null end))
/*
Id Name Remark
2 王二 无
*/
------解决方案--------------------
select a.* from
(
SELECT [AId],sum(case when [State]='true' then 0 else 1 end) as total