这是我到目前为止所尝试的内容。但它显示错误。
#1066 - 不是唯一的表/别名:'club'
select members.*, role.role_name, department.dept_name, club.club_name from members
join role on members.role_id=role.id
join department on members.dept_id=department.id
join club on members.club_id=club.id
join club on members.coordi_club_id = club.id
我该如何解决?
答案 0 :(得分:0)
请试试这个:
SELECT
members.*, role.role_name,
department.dept_name,
CL1.club_name
FROM
members
JOIN role ON members.role_id = role.id
JOIN department ON members.dept_id = department.id
JOIN club CL1 ON members.club_id = CL1.id
JOIN club CL2 ON members.coordi_club_id = CL2.id;
注意:第二次加入时,您需要为club
表提供别名。为了清楚起见,我使用了两个club
表(CL1,CL2)
的别名。
编辑:由于您要使用club
表的相同实例加入members
表,因此第二个连接会以此方式变为冗余。
相反,您应该使用以下查询:
SELECT
members.*, role.role_name,
department.dept_name,
CL1.club_name
FROM
members
JOIN role ON members.role_id = role.id
JOIN department ON members.dept_id = department.id
JOIN club CL1 ON members.club_id = CL1.id AND members.coordi_club_id = CL1.id;
答案 1 :(得分:0)
您不应在单个语句中多次加入表。擦除代码的最后一个连接部分,希望它能正常工作。
select members.*, role.role_name, department.dept_name, club.club_name from members
join role on members.role_id=role.id
join department on members.dept_id=department.id
join club on members.club_id=club.id ; // If you want to add another condition, just write AND keyword and specify in this line.
join club on members.coordi_club_id = club.id // You need not use this line