#1066 - 不是唯一的表/别名:' tb_X'

时间:2014-08-19 10:30:43

标签: mysql

当我尝试加入3张桌子时遇到了问题。

以下是我使用的mysql语法:

SELECT
FROM carecell
LEFT JOIN staff ON cc.id_pks = staff.id_emp
LEFT JOIN staff ON cc.id_wpks = staff.id_emp
INNER JOIN member ON member.id_member = staff.id_member

请帮帮我..我该怎么办才能修复语法?

3 个答案:

答案 0 :(得分:3)

SQL引擎无法区分staff子句中的两个from表。你需要给他们不同的名字。像这样:

FROM carecell cc LEFT JOIN
     staff s1
     ON cc.id_pks = s1.id_emp LEFT JOIN
     staff s2
     ON cc.id_wpks = s2 .id_emp INNER JOIN
     member m
     ON m.id_member = s2.id_member

答案 1 :(得分:1)

如果多次加入同一个表,则每次都需要为该表提供不同的别名,以便数据库引擎可以在它们之间有所不同

SELECT *
FROM carecell as cc
LEFT JOIN staff as s1 ON cc.id_pks = s1.id_emp
LEFT JOIN staff as s2 ON cc.id_wpks = s2.id_emp
INNER JOIN member as m ON m.id_member = s1.id_member

您已经使用了别名,因为您提供了carecell别名cc。在您的查询的最后一行,我加入了s1,但您必须决定采取哪一项 - s1s2

答案 2 :(得分:-1)

SELECT *
FROM carecell
LEFT JOIN staff t1 ON cc.id_pks = t1.id_emp
LEFT JOIN staff t2 ON cc.id_wpks = t2.id_emp
INNER JOIN member ON member.id_member = t1.id_member