我需要一些SQL帮助。我是交易的Java人,我甚至不知道如何提出这个问题。我有3张桌子,叫他们人,孩子,朋友。人是一个ID和一个名称:SQL外连接一个表与两个表
| id | name |
---------------------
| 1 | Joe |
让我们说孩子是一样的,但用FK回人
| id | personId | name |
-------------------------------------
| 1 | 1 | Frank |
| 2 | 1 | Dan |
和朋友是一回事
| id | personId | name |
-------------------------------------
| 1 | 1 | Will |
| 2 | 1 | Bob |
显然,这是我真正的问题的简化版本,但结构是相同的。我需要把所有这些数据在一个SQL拉这样的,我把它恢复
| personId | personName | childId | childName | friendId | friendName
------------------------------------------------------------------------------------------
| 1 | Joe | 1 | Frank | null | null
| 1 | Joe | 1 | Dan | null | null
| 1 | Joe | null | null | 1 | Will
| 1 | Joe | null | null | 2 | Bob
我曾尝试多个连接技术,但似乎无法破解它。 SQL从来不是我最好的主题。现在,我正与朋友和儿童名单<>所以显然这将工作太解析此为Java对象者:
| personId | personName | childId | childName | friendId | friendName
------------------------------------------------------------------------------------------
| 1 | Joe | null | null | null | null
| null | null | 1 | Frank | null | null
| null | null | 1 | Dan | null | null
| null | null | null | null | 1 | Will
| null | null | null | null | 2 | Bob
凡是允许干净的for循环在我的代码来构建这个。
谢谢!
我不是专家,但有两个全外连接上person.id = children.personId和person.id = friends.personId应该工作。你试过了什么,结果是什么? – mreff555
@ mreff555 this is tagged MySQL – Strawberry