我把所有父亲和母亲的人表都放在同一张孩子的桌子上
Pen-id firstname lastname gender father-id mother-id
10002 Ti Si-dao M null null
10025 Leonardo Vogel M 10002 10010
10010 Marissa Wilkes F null null
我需要查看以显示以下句子
Ti是Leonardo Vogel的父亲 玛丽莎是莱昂纳多沃格尔的母亲我使用了DDL但它没有用
CREATE VIEW parent AS
SELECT (FIRSTNAME) ||' '|| (LASTNAME)|| (CASE WHEN GENDER ='F' THEN ' is the mother of' ELSE 'is the father of' END) ||' '|| (FIRSTNAME) ||' '|| (LASTNAME) AS PARENTAGE
FROM P_PERSON ;
答案 0 :(得分:1)
此查询将根据需要为您提供输出但也请考虑以下注释。
select t2.firstname || ' Is Father Of '|| t1.firstname ||' '||t1.lastname
||' , '||t3.firstname ||' Is Mother Of ' ||t1.firstname ||' '||t1.lastname as PARENTAGE
from P_PERSON t1 , P_PERSON t2 , P_PERSON t3 where t1.father_id is not NULL
and t1.mother_id is not NULL and t1.father_id=t2.pen_id and t1.mother_id=t3.pen_id;
点击Fiddle
注意:上表是错误数据库设计的示例。理想的是你的 数据库应该在3NF以实现最大性能。如果可能的话 修改数据库设计。这会使
Child
和Parent
变得不同 对象/表减少头痛。
<强>更新强>
要在单独的行中输出,您可以使用union
select t2.firstname || ' Is Father Of '|| t1.firstname ||' '||t1.lastname
as PARENTAGE from P_PERSON t1 , P_PERSON t2
where t1.father_id is not NULL and t1.father_id=t2.pen_id
union
select t3.firstname ||' Is Mother Of ' ||t1.firstname ||' '||t1.lastname as
PARENTAGE from P_PERSON t1 , P_PERSON t3 where t1.mother_id is not NULL and
t1.mother_id=t3.pen_id;
点击Example