我正在尝试使用postgresql进行查询。该数据库包含两个关系:“王国”(其中包括一些英国国王)和“王者”(其中包含来自斯图尔特王朝的一些人)
关系“王国”包括国王的名字以及他的王国开始和结束的时间。关系“可辨”包括姓名,性别,出生和死亡。
我要询问的是国王去世时最老的国王。
通过我的查询,我在第3行(未输入)收到此错误: subquery has too many columns
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内的内容是正确的。
您将在子查询中投影 三 列,但在子句中仅对 其中一 列进行比较IN。在子查询中仅选择所需的列(r1.king)IN:
IN
r1.king
SELECT kingdom.king, dinasty.birth, dinasty.death FROM kingdom, dinasty WHERE kingdom.king = dinasty.name AND kingdom.king NOT IN ( SELECT DISTINCT R1.king 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 );