所以我的数据库中有这些表,其中包含以下数据:
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 调用,但如果可能的话,我宁愿一次性执行此操作。
最佳答案
安恩我明白了。真是太蠢了。
选择building_officer_membership.building_officer_membership_id、building_officer_membership.building_id、building_officer_membership.officer_id、GROUP_CONCAT(DISTINCT manager.full_name)作为manager_name、GROUP_CONCAT(DISTINCT manager.manager_id) AS manager_id从building_officer_membership加入building.building_id =building_officer_membership.building_id加入官员Officer.officer_id =building_officer_membership.officer_id 加入Officer_manager_membership 上Officer.officer_id =Officer_manager_membership.officer_id 加入经理ONOfficer_manager_membership.manager_id =Officer.Officer_id
按officer.officer_id分组
TIL group_concat 需要一个 group by 来知道如何组织结果,否则,它将返回尽可能少的结果。
关于MySQL 调用返回错误组合的结果,或者如果涉及的表为空,则返回全空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37996421/