mysql - SQL外连接一张表和两张表

标签 mysql sql

我需要一些 SQL 帮助。我是一名 Java 专业人士,我什至不知道如何提出这个问题。我有 3 张 table ,称他们为 Person、Children、Friends。人是一个 ID 和一个名字:

|   id   |    name    |
---------------------
|    1   |    Joe     |

假设 Children 是相同的,但 FK 回到 person

|   id   |    personId    |   name   |
-------------------------------------
|   1    |       1        |   Frank  |
|   2    |       1        |   Dan    |

和 friend 是一回事

|   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 对象 person 和 List<> of friends and children 所以显然这也可以工作:

 | 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 循环来构建它的东西。

谢谢!

最佳答案

select 
     p.id    as personId
  ,  p.name  as personName
  ,  c.id    as childId
  ,  c.name  as childName
  ,  null    as friendId
  ,  null    as friendName
from person p
  inner join child c
    on p.id = c.personId
union all 
select 
     p.id    as personId
  ,  p.name  as personName
  ,  null    as childId
  ,  null    as childName
  ,  f.id    as friendId
  ,  f.name  as friendName
from person p
  inner join friend f
    on p.id = f.personId;

rextester:http://rextester.com/BSPEC33394

返回:

+----------+------------+---------+-----------+----------+------------+
| personId | personName | childId | childName | friendId | friendName |
+----------+------------+---------+-----------+----------+------------+
|        1 | joe        | 1       | frank     | NULL     | NULL       |
|        1 | joe        | 2       | dan       | NULL     | NULL       |
|        1 | joe        | NULL    | NULL      | 1        | will       |
|        1 | joe        | NULL    | NULL      | 2        | bob        |
+----------+------------+---------+-----------+----------+------------+

关于mysql - SQL外连接一张表和两张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42448722/

相关文章:

php - 如何计算所有在场学生的百分比

php - 在错误消息后保留寄存器详细信息以防止重新输入数据

php - 跨域的所有页面都需要 HTTPS

sql - 如果为空则连接数据

sql - 同一事务的两个语句与单个语句的隔离级别

用于 "IN"语句的变量/字符串的 PHP PDO bindParam ...?

mysql - 将 MySQL 转储文件导入 MSSQL

php mysql IN 子句不适用于 CSV 变量。仅第一行受到影响

c# - 从 SQL Server 更新数据网格 - 更新过程不起作用

sql - 如何在 MS SQL Server 中减去两个连续的行?