【问题标题】:MySQL call is returning an incorrectly combined result, or a all null row if tables involved are emptyMySQL 调用返回错误组合的结果,或者如果涉及的表为空,则返回全空行
【发布时间】:2016-10-26 01:42:35
【问题描述】:

所以我的数据库中有这些表和这些数据:

building_officer_membership
+--------------------------------+-------------+------------+
| building_officer_membership_id | building_id | officer_id |
+--------------------------------+-------------+------------+
|                              1 |           1 |          1 |
|                              2 |           1 |          2 |
+--------------------------------+-------------+------------+

building
+-------------+-----------------+
| building_id | name            |
+-------------+-----------------+
|           1 | a_nice_building |
+-------------+-----------------+

officer
+------------+------------+-----------+
| officer_id | first_name | last_name |
+------------+------------+-----------+
|          1 | Brandon    | Thompson  |
|          2 | Mark       | Bobby     |
+------------+------------+-----------+

Manager
+------------+---------------+
| manager_id | full_name     |
+------------+---------------+
|          1 | Bill Lumbergh |
|          2 | Bob Page      |
+------------+---------------+

 officer manager membership
+-------------------------------+------------+------------+
| officer_manager_membership_id | officer_id | manager_id |
+-------------------------------+------------+------------+
|                             1 |          1 |          1 |
|                             2 |          2 |          2 |
+-------------------------------+------------+------------+

我有一个 MySQL 调用,它返回与建筑物相关联的每个官员(在 building_officer_membership 表中)。为了提供更多信息,我还在每个结果中列出了这些官员所在的经理。

当我进行这个 SQL 调用时,

SELECT building_officer_membership.building_officer_membership_id,
building_officer_membership.building_id,
building_officer_membership.officer_id,
GROUP_CONCAT(DISTINCT manager.full_name) as manager_name,
GROUP_CONCAT(DISTINCT manager.manager_id) AS manager_id
from building_officer_membership
JOIN building on building.building_id = building_officer_membership.building_id
JOIN officer on officer.officer_id = building_officer_membership.officer_id
JOIN officer_manager_membership on officer.officer_id = officer_manager_membership.officer_id
LEFT JOIN manager ON officer_manager_membership.manager_id = manager.manager_id

我希望有两个这样的结果:

+--------------------------------+-------------+------------+------------------------+------------+
| building_officer_membership_id | building_id | officer_id | manager_name           | manager_id |
+--------------------------------+-------------+------------+------------------------+------------+
|                              1 |           1 |          1 | Bill Lumbergh          | 1          |
|                              2 |           1 |          2 | Bob Page               | 2          |
+--------------------------------+-------------+------------+------------------------+------------+

相反,我得到了这个:

+--------------------------------+-------------+------------+------------------------+------------+
| building_officer_membership_id | building_id | officer_id | manager_name           | manager_id |
+--------------------------------+-------------+------------+------------------------+------------+
|                              1 |           1 |          1 | Bill Lumbergh,Bob Page | 1,2        |
+--------------------------------+-------------+------------+------------------------+------------+

这是将所有官员的经理组合成一个结果。

如果表是空的,并且我进行了 SQL 调用,我会得到这个,而不是一个空行:

+--------------------------------+-------------+------------+--------------+------------+
| building_officer_membership_id | building_id | officer_id | manager_name | manager_id |
+--------------------------------+-------------+------------+--------------+------------+
|                           NULL |        NULL |       NULL | NULL         | NULL       |
+--------------------------------+-------------+------------+--------------+------------+

我知道错误在 GROUP_CONCAT 部分,因为如果我删除这些,

select building_officer_membership.building_officer_membership_id,
    building_officer_membership.building_id,
    building_officer_membership.officer_id
    from building_officer_membership
    JOIN building on building.building_id = building_officer_membership.building_id
    JOIN officer on officer.officer_id = building_officer_membership.officer_id;
+--------------------------------+-------------+------------+
| building_officer_membership_id | building_id | officer_id |
+--------------------------------+-------------+------------+
|                              1 |           1 |          1 |
|                              2 |           1 |          2 |
+--------------------------------+-------------+------------+

我得到了我期望的结果,只是没有管理信息。

所以我不太确定这里发生了什么。我的加入是不是太贪心了?

我的 MySQL 版本是 5.1.73。由于我无法控制的原因,我无法升级到最新版本。在使用此 SQL 调用的 Web 服务代码中,我可以采取一种解决方法来解决涉及更多 SQL 调用的问题,如果可能的话,我宁愿一口气完成。

【问题讨论】:

  • 如果是mysql的,你可以删除sql-server的标签

标签: mysql join concatenation distinct


【解决方案1】:

Annnnd 我想通了。真是太傻了。

select building_officer_membership.building_officer_membership_id, building_officer_membership.building_id, building_officer_membership.officer_id, GROUP_CONCAT(DISTINCT manager.full_name) as manager_name, GROUP_CONCAT(DISTINCT manager.manager_id) AS manager_id from building_officer_membership 加入 building.building_id = building_officer_membership.building_id 加入官员officer.officer_id = building_officer_membership.officer_id 在officer.officer_id 上加入officer_manager_membership =officer_manager_membership.officer_id 在officer_manager_membership.manager_id 上加入经理=officer.officer_id

按officer.officer_id分组

TIL group_concat 需要 group by 才能知道如何组织结果,否则会尽量少返回。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2020-10-05
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多