使用双重联接选择多个 table 值

Selecting multiple table values with doubled joins

我有一个查询,它通过 JOIN 从不同的 table 中选择值。但是我想我现在遇到了一个问题,因为一个 table 需要用不同的名字重新加入,但不知怎么的,它不起作用。

基于社交网络结构的示例:

Table "users":

+--------+-----------+
| userid | username  | 
+--------------------|
| 1      | userOne   | 
| 2      | userTwo   | 
| 3      | userThree | 
+--------+-----------+

Table "posts":

+--------+---------+-------------------------------+
| postid | userid  | text                          |
+--------------------------------------------------|
| 102    | 1       | "Haha i'm User one"           |
| 103    | 1       | "And User one is the best"    |
| 104    | 3       | "I'm having fun with user two"|
+--------+---------+-------------------------------+

Table "usertags":

+--------+---------------+
| postid | tagged_userid | 
+------------------------|
| 104    | 2             | 
+--------+---------------+

这是我的查询:

SELECT posts.postid,
   posts.userid,
   posts.text, 
   users.username,
   IFNULL(GROUP_CONCAT(DISTINCT usertags.tagged_userid SEPARATOR ','), NULL) as 
taggedusers_id,
   IFNULL(GROUP_CONCAT(DISTINCT taggedusers.fullname SEPARATOR ','), NULL) as 
taggedusers_name, 
     FROM posts
JOIN users ON posts.userid = users.userid
LEFT JOIN usertags ON posts.postid = usertags.postid
LEFT JOIN users as taggedusers ON usertags.tagged_userid = users.userid
 GROUP BY posts.postid
 ORDER BY posts.postid DESC

这就是我得到的结果:

+--------+---------+---------------------------------------------------------------------+
| postid | userid  | text                          | username           | taggedusers_id |
+-----------------------------------------------------------------------|----------------|
| 102    | 1       | "Haha i'm User one"           | userOne            | NULL           |
| 103    | 1       | "And User one is the best"    | userOne            | NULL           |
| 104    | 3       | "I'm having fun with user two"| userThree          | 2
+--------+---------+-------------------------------+--------------------+----------------+

问题: 'taggedusers_name' 列出现了,但它总是显示 NULL。我希望它显示被标记的用户的用户名。 像这样,但是整体来说,输出很大

+---------------+-------------------+
| taggeduser_id | taggeduser_name   | 
+-----------------------------------|
| 2             | userTwo           | 
| 2,3           | userTwo,userThree | 
| NULL          | NULL              | 
+---------------+-------------------+

那么,这怎么可能呢?我需要做一个多重 SELECT 声明吗?我已经试过了,但我也失败了:/ 我很乐意提供帮助!

问题是您在没有别名的情况下再次引用 users。由于 users 已经被隐式地 INNER JOIN (参见这个问题 What is the default MySQL JOIN behaviour, INNER or OUTER?)所以 taggedusers table 将必须满足作者与相同的条件标记的用户 ID。

SELECT posts.postid,
   posts.userid,
   posts.text, 
   users.username,
   IFNULL(GROUP_CONCAT(DISTINCT usertags.tagged_userid SEPARATOR ','), NULL) as 
taggedusers_id,
   IFNULL(GROUP_CONCAT(DISTINCT taggedusers.fullname SEPARATOR ','), NULL) as 
taggedusers_name, 
FROM 
    posts
JOIN 
    users 
    ON posts.userid = users.userid
LEFT JOIN 
    usertags 
    ON posts.postid = usertags.postid
LEFT JOIN 
    users as taggedusers 
    ON usertags.tagged_userid = taggedusers.userid -- this is (I assume) what you meant
    -- ON usertags.tagged_userid = users.userid -- this is your problem
GROUP BY 
    posts.postid
ORDER BY 
    posts.postid DESC

避免这种情况的一种方法是始终使用别名 table;在这种情况下,您可以将 users 别名为 'author' 或类似的别名。

不同 ID 没有相同问题的原因是它位于正确连接的 table 上。