使用 Mysql Return 额外数据连接 3 个表

Joining 3 tables with Mysql Return Extra Data

我有 3 tables - 成员,活跃的,新的

成员table:

+--------+---------+------------+
| userid | adminid | logindate  | 
+--------+---------+------------+
| test6  | test3   | 03/04/2016 |
| test7  | test3   |            |
+--------+---------+------------+

活跃table:

+----+--------+---------+---------+
| id | userid | adminid | city    |
+----+--------+---------+---------+
| 1  | test6  | test3   | Chicago |
+----+--------+---------+---------+

新 table:

+----+--------+----------+
| id | userid | city     |
+----+--------+----------+
| 1  | test7  | New York |
+----+--------+----------+

我想找出属于管理员 ID (test3) 的所有成员,无论他们是活跃的还是新的。 mysql代码:

    SELECT active.id, active.userid, active.city, members.logindate,
 new.id as a, new.userid as b, new.city as c 
    FROM members
    LEFT JOIN active
    ON members.adminid = active.adminid
    LEFT JOIN new
    ON members.userid = new.userid
    WHERE members.adminid = 'test3'

我期望的是 2 行记录:

+----+--------+---------+------------+---+-------+----------+
| id | userid | city    | logindate  | a |  b    | c        |
+----+--------+---------+------------+---+-------+----------+
| 1  |test6   | Chicago | 03/04/2016 |   |       |          |
+----+--------+---------+------------+---+-------+----------+
|    |        |         |            | 1 | test7 | New York |
+----+--------+---------+------------+---+-------+----------+

相反,我得到了:

+----+--------+---------+------------+---+-------+----------+
| id | userid | city    | logindate  | a |  b    | c        |
+----+--------+---------+------------+---+-------+----------+
| 1  |test6   | Chicago | 03/04/2016 |   |       |          |
+----+--------+---------+------------+---+-------+----------+
| 1  |test6   | Chicago |            | 1 | test7 | New York |
+----+--------+---------+------------+---+-------+----------+

第一行的数据(来自 Active table)在第二行重复。我究竟做错了什么?感谢您的帮助。提前致谢。

你应该 joinuserid 而不是 adminid:

SELECT active.id, active.userid, active.city, members.logindate,
     new.id as a, new.userid as b, new.city as c 
FROM members
    LEFT JOIN active
        ON members.userid = active.userid
    LEFT JOIN new
        ON members.userid = new.userid
WHERE members.adminid = 'test3'