0
孫子我有一個名爲person(ID,fname,Age,Gender,parentID)
, 誰能告訴如何顯示誰沒有盛大的兒童在SQL個人表?顯示錶的人與SQL
孫子我有一個名爲person(ID,fname,Age,Gender,parentID)
, 誰能告訴如何顯示誰沒有盛大的兒童在SQL個人表?顯示錶的人與SQL
這應該做的伎倆,但如果你有很多的人有很多孩子可能會有點慢:
SELECT DISTINCT p.*
FROM person p
LEFT JOIN person c ON c.parentID = p.ID
LEFT JOIN person gc on gc.parentID = c.ID
WHERE gc.ID IS NULL
SELECT *
FROM person
WHERE
ID NOT IN (
SELECT id
FROM (
SELECT p.*,c.id as childID,g.id AS grandChildId
FROM person p
LEFT JOIN person c
ON c.parentID = p.ID
LEFT JOIN person g
ON g.parentID = c.ID
) AS person_relation
WHERE grandChildId
is NOT NULL
創建臨時表可以是有益的,但對於單個查詢這將工作。