SQL: subquery has too many columns

后端 未结 2 2405
暗喜
暗喜 2020-12-31 00:26

I\'m trying to make a query with postgresql. The database contains two relations: \"kingdom\", which includes some english kings, and \"dinasty\", which contains some people

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 01:03

    You are projecting three columns in your subquery, but comparing a single one of them in the IN clause. Select just the required column (r1.king) for the IN in the subquery:

    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
    );
    

提交回复
热议问题