SQL查询查找对象的孙代

时间:2018-07-05 23:11:39

标签: mysql database id

假设我有一个这样的表:

insert into orders(name,`order`,one,two,three) 
select name, max(`order`)+1 , 3, 'banana', true
from orders 
where name='stan'
group by name;

它存储着人们及其亲子关系。我想知道查询将如何返回孙子女最多的人的姓名和姓氏。我完全不知道从哪里开始。

3 个答案:

答案 0 :(得分:0)

#table list of fathers and their kid names

CREATE VIEW father AS SELECT  A.mom_id, B.name, B.surname FROM tablename A JOIN tablename B ON A.id = B.dad_id FROM tablename A JOIN tablename B ON A.id = B.dad_id

#table list of mothers and their kid names

CREATE VIEW mother AS SELECT A.mom_id, B.name, B.surname FROM tablename A JOIN tablename B ON A.id = B.dad_id FROM FROM tablename A JOIN tablename B ON A.id = B.mom_id

#table of grandpas link with their sons who have kids and count how many there are, 
#each father name will be repeated if they have more than one kids, so if we count the number father 
#names attach to grandpa names, we essentially find the number of grandkids. I got a little 
#lazy here and use order by greatest count instead of returning the max count to find the grandpa with the highest grand kids.
**#This will only give you # of kids produce by your sons. You also need to add the kids produce by your daughters.**

SELECT  T.id, T.name, T.surname, father.name, COUNT(father.name) FROM father JOIN tablename T ON tablename.id = father.dad_id GROUP BY T.id ORDER BY DESC;

答案 1 :(得分:0)

要使孩子的父母最多,所有人都在同一数据表中

类似这样的东西,再加上有效的SQL提琴。 希望这会有所帮助!


SQL__按孩子数/分数排序的父母列表

SELECT
  parents.parent as 'parent',
  MAX(parents.count) as 'score'
FROM (
  SELECT
    CONCAT(t2.name, " ", t2.surname) as 'parent',
    count(t1.parent_id) as 'count'
  FROM (
    -- LIST parent id, kid id
      -- * list of unique pairs: dad_id and id, 
      -- * also mom_id and id
    SELECT DISTINCT 
       dad_id as 'parent_id', id,
       name, surname
    FROM family
    UNION
    SELECT DISTINCT 
       mom_id as 'parent_id', id,
       name, surname
    FROM family
  ) as t1
  INNER JOIN family AS t2 ON t1.parent_id = t2.id
  GROUP BY parent_id
  ORDER BY parent_id
) as parents
GROUP BY parents.parent
ORDER BY 'score';

此外,如果您只想返回第一个结果(即最亲子的),则可以在末尾加上“ LIMIT 1”。

SQL Fiddle

答案 2 :(得分:0)

首先,找到有孩子的人并将其与孩子一起保存到临时表中。然后找到孩子也有孩子的人。

SQL脚本:

-- Create table
CREATE TABLE #Family
(
    id INT,
    name VARCHAR(30),
    surname VARCHAR(30),
    dad_id INT,
    mom_id INT
)

--insert data
INSERT INTO #Family (id, name, surname, dad_id, mom_id) VALUES (1, 'John', 'Smith', 2, 3)
INSERT INTO #Family (id, name, surname, dad_id, mom_id) VALUES (2, 'Arnold', 'Smith', 7, 8)
INSERT INTO #Family (id, name, surname, dad_id, mom_id) VALUES (3, 'Margaret', 'Smith', 5, 6)
INSERT INTO #Family (id, name, surname, dad_id, mom_id) VALUES (4, 'Junior', 'Smith', 1, 9)


-- Find people With child and save it into temporary table #FGTwo (family generation two)
SELECT * INTO #FGTwo FROM
(SELECT f.id, f.name, f.surname, f.dad_id, f.mom_id, 
fd.id cid, fd.name cname, fd.surname csurname, fd.dad_id cdad_id, fd.mom_id cmom_id
FROM #Family f
INNER JOIN #Family fd ON fd.dad_id = f.id
UNION ALL
SELECT f.id, f.name, f.surname, f.dad_id, f.mom_id, 
fm.id cid, fm.name cname, fm.surname csurname, fm.dad_id cdad_id, fm.mom_id cmom_id
FROM #Family f
INNER JOIN #Family fm ON fm.mom_id = f.id) AS FG2

--Uncomment next line to see people With their child
-- select * from #FGTwo

-- Find people With Grandchild and save it into temporary table #PeopleWithGrandchildren
SELECT * INTO #PeopleWithGrandchildren FROM
(SELECT f2.id, f2.name, f2.surname
from #FGTwo f2
INNER JOIN #Family fd ON fd.dad_id = f2.cid
UNION ALL
SELECT f2.id, f2.name, f2.surname
FROM #FGTwo f2
INNER JOIN #Family fm ON fm.mom_id = f2.cid) pwg

--Uncomment next line to see people With their child
-- select * from #PeopleWithGrandchildren

-- Finds all people having the same maximal number of grandchildren
DECLARE @MaxCount INT
SET @MaxCount = (SELECT TOP 1 COUNT(id) AS MaxCount FROM #PeopleWithGrandchildren GROUP BY id ORDER BY MaxCount DESC)

SELECT name, surname FROM #PeopleWithGrandchildren 
WHERE id IN 
(SELECT GC.id FROM 
(SELECT id, COUNT(id) AS GCount FROM #PeopleWithGrandchildren GROUP BY id) GC
WHERE GCount = @MaxCount)
--If you want to select only one use the "SELECT TOP 1 " instead of "SELECT *"

-- drop all temporary tables
drop table #Family
drop table #FGTwo
drop table #PeopleWithGrandchildren

输出:

name        surname
Arnold      Smith
Margaret    Smith