使用doubled连接选择多个表值

时间:2017-10-25 15:30:52

标签: mysql list join concat group-concat

我有一个查询,通过JOIN从不同的表中选择值。但是我觉得我现在有一个问题,因为一个表需要再次使用不同的名称加入,但不知何故它不起作用。

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

表“用户”:

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

表“帖子”:

+--------+---------+-------------------------------+
| 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"|
+--------+---------+-------------------------------+

表“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语句吗?我已经试过了,但我也失败了:/ 我很乐意帮忙!

1 个答案:

答案 0 :(得分:0)

问题是您在没有别名的情况下再次引用users。由于users已经隐式INNER JOIN(请参阅此问题What is the default MySQL JOIN behaviour, INNER or OUTER?),因此taggedusers表必须满足作者与标记用户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

避免这种情况的一种方法是始终对表进行别名;在这种情况下,您可以使用别名users作为'作者'或类似的东西。

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