2013-06-04 2 views
0

는 SQL 쿼리 (MySQL의) :가 공식화 도움이 필요 나는이 테이블 구조가

CREATE TABLE IF NOT EXISTS `person` (
    `ID` int(11) NOT NULL AUTO_INCREMENT, 
    `father` text NOT NULL, 
    `mother` text NOT NULL, 
    `name` text NOT NULL, 
    PRIMARY KEY (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ; 

데이터 :

(`ID`, `father`, `mother`, `name`) 
(1  ''  ''  'Robert Darwin'), 
(2  ''  ''  'Josiah Wedgwood'), 
(3  ''  ''  'Sarah Wedgwood'), 
(4  ''  ''  'Mary Howard'), 
(5  1   ''  'Erasmus Darwin'), 
(6  ''  ''  'Elizabeth Allen'), 
(7  2   3  'Josiah II'), 
(8  2   3  'Susannah Wedgwood'), 
(9  5   4  'Robert Waring Darwin'), 
(10  7   6  'Josiah III'), 
(11  7   6  'Emma Wedgwood'), 
(12  9   8  'Charles Robert'), 
(13  9   8  'Caroline Sarah Darwin'), 
(14  10  13  'Margaret Wedgwood'), 
(15  11  12  'William Erasmus'); 

내가되고 무엇이 필요 배우자 사람들의 목록을 수집하기를FIRST-DEGREE COUSIN입니까?

누구든지 MySQL 쿼리를 공식화하는 방법을 알려줄 수 있습니까?

+0

이것은 무엇을 시도 했는가? –

+0

나누고 정복하십시오. 주어진 한 사람의 배우자 (기술적으로 말해 배우자 목록)를 검색 할 수 있습니까? 그의 1 학년 사촌 목록을 검색 할 수 있습니까? – RandomSeed

+0

실제로 결과를 얻는 방법에 대한 정확한 논리를 얻지 못합니다. :/나는 mysql을 배우기 시작했다. 여러 개의 쿼리를 실행할 수 있습니까? – shiftescape

답변

0

비록이 질문이 숙제처럼 들리지만 나는 심사 위원이 아닙니다. 나는 내 마음에 운동으로 이것을하고있다 :

SELECT p1.father, p1.mother 
FROM person p1 
WHERE 
    (p1.father, p1.mother) IN (
     SELECT firstSiblingChild.ID AS ID1, secondSiblingChild.ID AS ID2 
     FROM (
      SELECT p1.ID AS ID1, p2.ID AS ID2 
      FROM person p1 
      INNER JOIN person p2 ON (p1.father = p2.father AND p1.mother = p2.mother) 
      WHERE 
       p1.father <> "" 
      AND 
       p1.mother <> "" 
      AND 
       p1.ID <> p2.ID 
     ) siblings 
     INNER JOIN person firstSiblingChild ON (siblings.ID1 = firstSiblingChild.father OR siblings.ID1 = firstSiblingChild.mother) 
     INNER JOIN person secondSiblingChild ON (siblings.ID2 = secondSiblingChild.father OR siblings.ID2 = secondSiblingChild.mother) 
    ) 
+0

좋은 생각을 해줘서 고맙지 만이 쿼리는 다음을 반환합니다. ** Charles Robert, Caroline Sarah Darwin, Margaret Wedgwood, William Erasmus **. 하지만 내 이해를 바탕으로 반환되는 올바른 기록은 다음과 같아야합니다. ** 엠마 웨지 우드, Charles Robert, Josiah III, Caroline Sarah Darwin **. 내 대답과 관련된 제안/의견은 큰 도움이됩니다. – shiftescape

+0

"Charles Robert", "Caroline Sarah Darwin", "Margaret Wedgwood"및 "William Erasmus"를 어떻게 얻었는지 잘 모르겠습니다. 이 쿼리는'Josiah III, Caroline Sarah Darwin'과'Emma Wedgwood, Charles Robert'를 의미하는'10,13'과'11,12 '를 반환합니다. – Mehran

+0

어쨌든, 당신의 아이디어는 정말로 도움이됩니다! 고마워 친구! :) – shiftescape

0

자녀가있는 사람은 결혼했고 (자녀가없는 사람은 결혼하지 않은 것으로 가정) 다음과 같이 할 수 있습니다. 그러나 아마도 그렇게 효율적이지는 않습니다. 그들은 조부모를 공유 할 수 있도록

SELECT DISTINCT f.name 
FROM (
SELECT a.ID AS Parent, c.ID AS GrandChild 
FROM person a 
INNER JOIN person b ON a.ID = b.father 
INNER JOIN person c ON b.ID = c.father 
UNION 
SELECT a.ID, c.ID 
FROM person a 
INNER JOIN person b ON a.ID = b.father 
INNER JOIN person c ON b.ID = c.mother 
UNION 
SELECT a.ID, c.ID 
FROM person a 
INNER JOIN person b ON a.ID = b.mother 
INNER JOIN person c ON b.ID = c.mother 
UNION 
SELECT a.ID, c.ID 
FROM person a 
INNER JOIN person b ON a.ID = b.mother 
INNER JOIN person c ON b.ID = c.father) y 
INNER JOIN (
SELECT a.ID AS Parent, c.ID AS GrandChild 
FROM person a 
INNER JOIN person b ON a.ID = b.father 
INNER JOIN person c ON b.ID = c.father 
UNION 
SELECT a.ID, c.ID 
FROM person a 
INNER JOIN person b ON a.ID = b.father 
INNER JOIN person c ON b.ID = c.mother 
UNION 
SELECT a.ID, c.ID 
FROM person a 
INNER JOIN person b ON a.ID = b.mother 
INNER JOIN person c ON b.ID = c.mother 
UNION 
SELECT a.ID, c.ID 
FROM person a 
INNER JOIN person b ON a.ID = b.mother 
INNER JOIN person c ON b.ID = c.father) x 
ON y.Parent = x.Parent 
INNER JOIN person z ON z.father = x.GrandChild AND z.mother = y.GrandChild 
INNER JOIN person f ON f.ID = z.father OR f.ID = z.mother 

각 사람과 그랜드 아이를 얻을이 개 큰 서브 선택이있다는 ID 필드에 합류했다. 이것들은 person 테이블에 조인되어 하나의 subselect를 아버지로, 하나를 부모로 취급하여 부모가 웅장한 부모를 공유하는 사람들을 얻은 다음 실제 테이블에 다시 결합하여 실제 부모를 얻습니다.

EDIT - 짧은 코드.

나는 JOIN의 ON 절에서 OR을 사용하여 위의 값을 줄이므로 결합을 피할 수 있습니다. 나는 JOIN에서 OR을 사용하는 것을 싫어하지만 가독성을 높입니다.

SELECT DISTINCT f.name 
FROM (
SELECT a.ID AS Parent, c.ID AS GrandChild 
FROM person a 
INNER JOIN person b ON a.ID = b.father OR a.ID = b.mother 
INNER JOIN person c ON b.ID = c.father OR b.ID = c.mother 
) y 
INNER JOIN (
SELECT a.ID AS Parent, c.ID AS GrandChild 
FROM person a 
INNER JOIN person b ON a.ID = b.father OR a.ID = b.mother 
INNER JOIN person c ON b.ID = c.father OR b.ID = c.mother 
) x 
ON y.Parent = x.Parent 
INNER JOIN person z ON z.father = x.GrandChild AND z.mother = y.GrandChild 
INNER JOIN person f ON f.ID = z.father OR f.ID = z.mother 
+0

고마워요!나는 그것을 잘 이해할 수 있도록 그것을 공부할 것입니다.이 훌륭한 아이디어를 가져 주셔서 감사합니다! :) – shiftescape