[英]How to select two columns from one table that are connected to a column in another table in SQL?
[英]SQL Select two columns from one table translated by a column from another table
我有一個表ID,名字和姓氏。 我還有一張孩子和父母的桌子。 我想使用ID表將子表/父表從int轉換為nvarchar。 在此示例中,我們有兩個人,馬克和雷·史密斯。 雷是馬克的父母。 雷沒有父母。
People_Table
ID|First|Last
--+-----+----
0 |NULL|NULL
10|Mark|Smith
15|Ray |Smith
Parent_Child_Table
Child|Parent
-----+------
10 | 15
15 | 0
我想得到
First|Last|First|Last
-----+-----+----+-----
Mark|Smith|Ray|Smith
Ray|Smith|NULL|NULL
例如,我嘗試使用INNER JOIN,但這僅給我孩子的名字。 使用兩個select語句使我只列出所有人員,但並不能保持結構。 有任何想法嗎?
很抱歉編輯不當(或缺少它),這是我第一次在這里發布,我很着急。 我希望不要混淆。
select p.first, p.last, c.first, c.last
from parent_child_table pc
inner join people_table p on p.id = pc.parent
inner join people_table c on c.id = pc.child
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.