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

时间:2015-10-14 09:58:54

标签: mysql

我在这里收到错误,#1066 - Not unique table/alias: 'fx_users'

我被困在哪里?

这是我的疑问:

SELECT from_date
    , to_date
    , fx_users.username
    , fx_leave_type.NAME
    , STATUS
FROM fx_leave_assign
INNER JOIN fx_leave_type
    ON fx_leave_type.id = fx_leave_assign.leavetype_id
INNER JOIN fx_users
    ON fx_users.id = fx_leave_assign.user_id
INNER JOIN fx_users_attendance
    ON fx_users_attendance.user_id = fx_users.id
INNER JOIN `fx_account_details`
    ON fx_account_details.user_id = fx_users_attendance.user_id
INNER JOIN `fx_departments`
    ON fx_departments.deptid = fx_account_details.department
INNER JOIN `fx_users`
    ON fx_users.id = fx_users_attendance.user_id
WHERE fx_departments.deptid IN (1)
    AND fx_users.id IN (5)
    AND DATE (fx_users_attendance.clock_in) >= "2015-09-01"
    AND DATE (fx_users_attendance.clock_in) <= "2015-09-30"
    AND fx_leave_assign.leavetype_id = "1";

2 个答案:

答案 0 :(得分:2)

原因很简单,在您的查询中,您加入fx_users两次。您需要为它们添加别名。

SELECT from_date,
       to_date,
       fu1.username,
       fx_leave_type.name,
       STATUS
FROM fx_leave_assign
JOIN fx_leave_type ON fx_leave_type.id = fx_leave_assign.leavetype_id
JOIN fx_users AS fu1 ON fu1.id = fx_leave_assign.user_id
JOIN fx_users_attendance ON fx_users_attendance.user_id=fu1.id
JOIN `fx_account_details` ON fx_account_details.user_id = fx_users_attendance.user_id
JOIN `fx_departments` ON fx_departments.deptid = fx_account_details.department
JOIN `fx_users` AS fu2 ON fu2.id = fx_users_attendance.user_id
WHERE fx_departments.deptid IN(1)
  AND fu1.id IN(5)
  AND date(fx_users_attendance.clock_in) >= "2015-09-01"
  AND date(fx_users_attendance.clock_in) <= "2015-09-30"
  AND fx_leave_assign.leavetype_id="1"

此外,您应该使用from_dateto_dateSTATUS列。有多个表,你可以得到不明确的名称。

答案 1 :(得分:1)

您要加入表格fx_users两次。如果这样做,请为表使用不同的别名,以允许DB区分这两者。

... JOIN `fx_users` as users_1 on ...