SQL查询组和所有

时间:2017-08-02 14:36:58

标签: mysql sql database group-by self-join

我有一张桌子:

<div [innerHTML]="the.path.to.property"></div>

现在,我需要找到所有接受过子女教育的父母,即受过教育的孩子=&#39;是的。

与上述情况一样,父母&#39; P3&#39;

任何人都可以建议查询来获取此

4 个答案:

答案 0 :(得分:3)

select parent, 
       sum(case when educated='YES' then 1 else 0 end) as sum_educated,
       count(*) as count_all
from t
group by parent
having count_all=sum_educated

答案 1 :(得分:1)

查找有“No&#39; No&#39;并使用NOT IN从结果中删除它们:

SELECT parent
FROM table
WHERE parent NOT IN (SELECT parent FROM table WHERE Educated = 'No')
GROUP BY parent

答案 2 :(得分:1)

我会这样做:

select parent
from t
group by parent
having max(educated) = min(educated) and max(educated) = 'YES';

如果educated可能是NULL,则逻辑稍微复杂一些。

实际上,如果值只是'YES''NO',您可以做得更简单:

select parent
from t
group by parent
having min(educated) = 'YES';

答案 3 :(得分:-2)

从[YourTable]中选择* Educated =&#39;是&#39;

如果您只想显示父列 然后从[YourTable]中选择Parent Educated =&#39;是&#39;