我想用postgresql進行查詢。 該數據庫包含兩個關係:「王國」,其中包括一些英國國王,和「dinasty」,其中包含一些來自斯圖爾特王朝的人SQL:子查詢有太多列
關係「王國」包括國王的名字,當他的王國開始和結束。關係「dinasty」包括姓名,性別,出生和死亡。
我想要查詢的是當他去世時最老的國王。
隨着我的查詢我在LINE 3(NOT IN)收到此錯誤:subquery has too many columns
這是查詢:
SELECT kingdom.king, dinasty.birth, dinasty.death
FROM kingdom, dinasty
WHERE kingdom.king = dinasty.name AND kingdom.king NOT IN
(
SELECT DISTINCT R1.king, R1.birth, R1.death
FROM
(
SELECT DISTINCT R1.king, D1.birth, D1.death
FROM kingdom AS R1, dinasty AS D1, dinasty AS D2
WHERE R1.king=D1.name
) AS R1,
(
SELECT DISTINCT R1.king, D1.birth, D1.death
FROM kingdom AS R1, dinasty AS D1, dinasty AS D2
WHERE R1.king=D1.name
) AS R2
WHERE R1.death-R1.birth < R2.death-R2.birth
);
裏面是什麼的NOT IN是正確的。
它不會影響您的代碼,但請正確拼寫王朝。 –