有关嵌套查询主题的SQL查询

时间:2017-06-29 17:44:59

标签: mysql

查询显示所有“父亲(丈夫)”名称,按字母顺序从Guardian表中显示其“妻子”的总数(使用嵌套查询查找妻子数)。

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用自我加入和分组

 select a.guardianName as husband_name, count(*) as count_of_wives
 from my_table a 
 left join my_table b  on b.husband_id = a.guardianId
 where a.husband_id is null 
 group by a.guardianName