2016-03-05 56 views
0

我有3个表 - 会员,活动,新加入3代表与MySQL返回额外数据

成员表:

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

活动表:

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

新表:

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

我想找出所有的memb属于管理员标识(test3)的ers,无论它们是主动还是新的。 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 | 
+----+--------+---------+------------+---+-------+----------+ 

从第一行中的数据(从活动表)在第二行重复。我究竟做错了什么?感谢您的帮助。提前致谢。

回答

1

您应该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'