MYSQL 1:M递归关系

时间:2017-12-22 23:18:30

标签: sql sql-server tsql

我一直在试图弄清楚这个问题,我已经创建了一个表并尝试制作一个能够回答这个问题的Select语句

  

辛普森家族的母亲是谁?从最旧的列出它们   最小的。

根据我目前的数据,我不知道如何只用一张桌子来做这件事。

CREATE TABLE person
(
persID           INT IDENTITY(1,1),
persFName        VARCHAR(40) NOT NULL,
persLName        VARCHAR(40) NOT NULL,
persGender       CHAR(1) NOT NULL,
persDOB          DATE NOT NULL,
persDOD          DATE ,
fatherID         INT ,
motherID         INT ,

CONSTRAINT pk_person_persID PRIMARY KEY(persID),
CONSTRAINT fk_person_fatherID FOREIGN KEY (fatherID) REFERENCES person(persID),
CONSTRAINT fk_person_motherID FOREIGN KEY (motherID) REFERENCES person(persID),


);

这是我的数据

INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ('Abraham', 'Simpson', 'M', '1944-01-15', '2015-07-21',NULL,NULL)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ('Mona', 'Simpson', 'F', '1946-09-22', NULL,NULL,NULL)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Herb', 'Simpson', 'M', '1963-11-21',NULL,1,2)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Homer', 'Simpson', 'M', '1965-05-19',NULL,1,2)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Clancy', 'Bouvier', 'F', '1945-02-12',NULL,NULL,NULL)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Jackie', 'Bouvier', 'M', '1945-12-01','2016-05-15',NULL,NULL)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Marge', 'Simpson', 'F', '1966-05-18',NULL,6,5)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Patty', 'Bouvier', 'F', '1964-01-08',NULL,6,5)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Selma', 'Bouvier', 'F', '1969-03-01',NULL,6,5)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Bart', 'Simpson', 'M', '1990-01-01',NULL,4,7)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Lisa', 'Simpson', 'F', '1992-05-15',NULL,4,7)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Maggie', 'Simpson', 'F', '1997-11-28',NULL,4,7)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Ling', 'Bouvier', 'M', '2000-04-02',NULL,NULL,9)

2 个答案:

答案 0 :(得分:0)

这应该会给你你想要的东西,但我不确定它是否是最聪明的方式!我希望这有帮助。

   SELECT DISTINCT T2.motherID, 
                    T1.persID, 
                    T1.PersFName, 
                    T1.PersLName,
                    T1.PersGender,
                    T1.PersDOB,
                    T1.PersDOD,
                    T1.FatherID,
                    T1.MotherID
    FROM person AS T1
    LEFT JOIN person  AS T2 ON T1.persID = T2.motherID
    WHERE T2.motherID IS NOT NULL
    AND T1.persLName = 'Simpson'
    ORDER BY T1.persDOB 

答案 1 :(得分:0)

试试这个:

SELECT *
FROM person
WHERE persID IN (SELECT DISTINCT motherID FROM person)
    AND persLNAme = 'Simpson'
ORDER BY persDOB ASC;

这个想法是让所有的母亲和#39; ID然后从基表中提取它们。然后我们使用ORDER BY子句对结果进行排序。

enter image description here