多个联接的奇怪SQL问题

时间:2016-09-06 22:05:36

标签: mysql sql left-join

我目前正在为应用程序构建API,但在使用特定查询时遇到了一些问题。

这是我原来的疑问:

SELECT `users`.`id`, 
       unclaimed_users.id AS unclaimed_id, 
       `users`.`firstname`, 
       `users`.`lastname`, 
       `unclaimed_users`.`mobile_number`, 
       `group_members`.`status`, 
       `user_images`.`profile_image_main` 
FROM   `group_members` 
       LEFT JOIN `users` 
              ON `users`.`id` = `group_members`.`user_id` 
                 AND `group_members`.`unclaimed_user` = 0 
       LEFT JOIN `user_images` 
              ON `user_images`.`id` = `group_members`.`user_id` 
                 AND `group_members`.`unclaimed_user` = 0 
       LEFT JOIN `unclaimed_users` 
              ON `unclaimed_users`.`id` = `group_members`.`user_id` 
                 AND `group_members`.`unclaimed_user` = 1 
WHERE  `group_members`.`group_id` = 1 
ORDER  BY `group_members`.`created_at` ASC

当我执行上面的查询时,我收到了我期望的两条记录,但是,从unclaimed_user表中提取的任何字段都返回一个空字符串(非空),而不是返回值。

见这里:

+------+--------------+-----------+----------+---------------+---------+--------------------+
| id   | unclaimed_id | firstname | lastname | mobile_number | status  | profile_image_main |
+------+--------------+-----------+----------+---------------+---------+--------------------+
|    1 |         NULL | Ben       | Carey    | NULL          | active  | NULL               |
| NULL |            0 | NULL      | NULL     |               | pending | NULL               |
+------+--------------+-----------+----------+---------------+---------+--------------------+

但是,当我将SELECT替换为SELECT *时,它会返回预期的所有字段以及预期值。例如,mobile_number返回与相关无人认领用户关联的手机号码。

我做错了什么?我无法解决问题,以及为什么没有返回值。

我尝试删除所有字段,并且只包括未声明的用户表中的一个字段,例如

SELECT unclaimed_users.id FROM etc...

以上返回0,但应返回1 ...

请务必注意id表和mobile_number表中都存在usersunclaimed_users

更新

我目前正在将小提琴放在一起,但事实证明它比我想象的更难,因为我还没有成功地重新创造这个问题......

与此同时,我简化了仍未返回其意图的查询:

SELECT `unclaimed_users`.`mobile_number` 
FROM   `group_members` 
       LEFT JOIN `unclaimed_users` 
              ON `unclaimed_users`.`id` = `group_members`.`user_id` 
                 AND `group_members`.`unclaimed_user` = 1 
WHERE  `group_members`.`group_id` = 1 

以上内容将返回以下内容

+---------------+
| mobile_number |
+---------------+
| NULL          |
|               |
+---------------+

应该返回时:

+---------------+
| mobile_number |
+---------------+
| NULL          |
| +447777779999 |
+---------------+

如果我将SELECT unclaimed_users.mobile_number替换为SELECT *,则会返回正确的数据,以便加入正常。我无法理解为什么我无法引用字段mobile_number

更新2

我注意到它在我执行以下操作时有效:

SELECT `unclaimed_users`.`mobile_number` 
FROM   `group_members` 
       LEFT JOIN `unclaimed_users` 
              ON `unclaimed_users`.`id` = `group_members`.`user_id` 
                 AND `group_members`.`unclaimed_user` = 1 
WHERE  `group_members`.`unclaimed_user` = 1 -- This is the bit I changed

因为上面忽略了表格中的第一条记录(unclaimed_user=0,它似乎有效......

1 个答案:

答案 0 :(得分:1)

因为你没有提供太多的测试......

SELECT `users`.`id`, 
   unclaimed_users.id AS unclaimed_id, 
   `users`.`firstname`, 
   `users`.`lastname`, 
   `unclaimed_users`.`mobile_number`, 
   `group_members`.`status`,
   `group_members`.`user_id`,
   `group_members`.`unclaimed_user`,
   `user_images`.`profile_image_main` 
FROM   `group_members` 
   LEFT JOIN `users` 
          ON `users`.`id` = `group_members`.`user_id` 
             AND `group_members`.`unclaimed_user` = 0 
   LEFT JOIN `user_images` 
          ON `user_images`.`id` = `group_members`.`user_id` 
             AND `group_members`.`unclaimed_user` = 0 
   LEFT JOIN `unclaimed_users` 
          ON `unclaimed_users`.`id` = `group_members`.`user_id` 
             AND `group_members`.`unclaimed_user` = 1 
WHERE  `group_members`.`group_id` = 1 
ORDER  BY `group_members`.`created_at` ASC

我不确定是否有一个好的答案,我只是无法添加评论。