MySQL链内部JOIN错误

时间:2017-12-03 17:40:07

标签: mysql sql phpmyadmin

当我登录用户时,我想检索他的个人信息,例如他的班级和团队。我设法检索了这些课程,但是当我尝试检索这个课程时,它没有工作。

我有3个表(用户,团队和类)和2个连接表(teams_users和classes_users)。

以下关系:

classes (id_classe) <--> classes_users (classe_id, user_id ) <--> users (id_user)
teams (id_team) <--> teams_users (team_id, user_id) <--> users (id_user)

这有效

SELECT * FROM classes `c` 
INNER JOIN (SELECT * FROM classes_users `cu` 
INNER JOIN (SELECT * FROM users WHERE (users.email = 'tata@gmail.com'))
`u` ON (u.id_user = cu.user_id)) 
`ui` ON (ui.classe_id = c.id_classe)

但是当我尝试在下面添加另一个连接时,它没有用。

SELECT * FROM teams_users `tu` 
INNER JOIN (SELECT * FROM classes `c` 
INNER JOIN (SELECT * FROM classes_users `cu` 
INNER JOIN (SELECT * FROM users WHERE (users.email = 'tata@gmail.com')) 
`u` ON (u.id_user = cu.user_id))
`ui`ON (ui.classe_id = c.id_classe)) 
`wc` ON (wc.id_user = tu.user_id)

1 个答案:

答案 0 :(得分:1)

为什么所有的括号和子查询?我认为这就是你要做的事情:

SELECT *
FROM teams_users tu INNER JOIN 
     users u
     ON u.id_user = tu.user_id INNER JOIN
     class_users cu
     ON u.id_user = cu.user_id INNER JOIN
     classes c
     ON cu.classe_id = c.id_classe
WHERE u.email = 'tata@gmail.com';