Home  >  Article  >  Database  >  取满足所有条件的记录

取满足所有条件的记录

WBOY
WBOYOriginal
2016-06-07 15:22:431036browse

Table_A表是用户表,Table_B是条件表,每个用户对应多个条件,并且用户对应条件的状态有可能是true或者false,现在需要找出在Table_B中所有条件都是true的用户。 Table_A表的数据 Table_B表的数据 通过分析A表和B表的数据可以得出,只有用户王二满足所有条件

Table_A表是用户表,Table_B是条件表,每个用户对应多个条件,并且用户对应条件的状态有可能是true或者false,现在需要找出在Table_B中所有条件都是true的用户。

Table_A表的数据

Table_B表的数据

通过分析A表和B表的数据可以得出,只有用户“王二”满足所有条件,期望的结果就是 

实现SQL语句

select a.* from
(
  SELECT [AId],sum(case when [State]='true' then 0 else 1 end) as total
  FROM [Table_B]
  group by [AId]
) b inner join [Table_A] a on b.AId=a.Id 
where total =0
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn