寻找顶级大家庭

时间:2018-04-21 13:47:43

标签: mysql

我有7列

的数据库

**

  

名字||出生日期||地方出生|| name-of_father ||性别||   最后名称

**

我尝试了什么:

String SQL="select name-of_father,last-name,Count(*) from tab a
                where name-of_father in ( select name-of_father from tab b
                                       where a.name-of_father=b.name-of_father);

我想要的是如何找到?

1-Top Big Family(选择姓名父亲,姓氏,计数(子女数)。

2 - 拥有最多男孩的家庭。

如果有人帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:1)

  

Top Big Family(选择name-of_father,姓氏,Count(数量)   儿童的)。

查询应该是类似的。

<强>查询

 SELECT 
   father.`First-name`
 , father.`last-name`
 , COUNT(*) AS number_of_childeren
FROM 
 table father
INNER JOIN
 table childeren
ON
 father.`First-name` = childeren.`name-of_father`
ORDER BY
 COUNT(*) DESC
LIMIT 1
  

拥有最多男孩的家庭。

查询应该是类似的。

<强>查询

SELECT 
   `last-name`
 , COUNT(*) AS number_of_males
FROM 
 table
WHERE 
 sex = 'male'
GROUP BY 
  `last-name`
ORDER BY
  COUNT(*) DESC
LIMIT 1