晚上好! 我有问题,因为我不能在WHERE中使用MAXGROWTHPARENT值 ORDER BY claused。 我写了这个问题:
SELECT PERSON.*,
(
SELECT MAX(PARENT.GROWTH)
FROM MAN PARENT
WHERE
(PARENT.ID = PERSON.ID_FATHER)
OR
(PARENT.ID = PERSON.ID_MOTHER)
) AS MAXGROWTHPARENT
FROM MAN PERSON
WHERE PERSON.GROWTH >
(
SELECT MAX(PARENT.GROWTH)
FROM MAN PARENT
WHERE
(PARENT.ID = PERSON.ID_FATHER)
OR
(PARENT.ID = PERSON.ID_MOTHER)
)
ORDER BY (PERSON.GROWTH -
(
SELECT MAX(PARENT.GROWTH)
FROM MAN PARENT
WHERE
(PARENT.ID = PERSON.ID_FATHER)
OR
(PARENT.ID = PERSON.ID_MOTHER)
)
);
我的代码看起来非常难看。你能告诉我如何使用MAXGROWTHPARENT吗? 在WHERE和ORDER BY子句中?
答案 0 :(得分:1)
我认为两个单独的连接可能会更好:
select p.*, greatest(pf.growth, pm.growth) as MAXGROWTHPARENT
from man p left join
man pf
on p.id_father = pf.id_father left join
man pm
on p.id_mother = pm.id_mother
order by p.growth - greatest(pf.growth, pm.growth);
注意:如果联接可能不匹配,您可能更喜欢:
select p.*, greatest(coalesce(pf.growth, pm.growth), coalesce(pm.growth, pf.growth)) as MAXGROWTHPARENT
from man p left join
man pf
on p.id_father = pf.id_father left join
man pm
on p.id_mother = pm.id_mother
order by p.growth - greatest(coalesce(pf.growth, pm.growth), coalesce(pm.growth, pf.growth));