SQLSTATE 错误语法错误或访问冲突:1066 不唯一 table/alias:'users_sessions'

SQLSTATE Error Syntax error or access violation: 1066 Not unique table/alias: 'users_sessions'

我在使用连接进行简单查询时遇到错误。

我的查询:

SELECT users_sessions.user_id AS users_sessions_user_id,
       users.last_name AS users_last_name,
       users.first_name AS users_first_name 
FROM prefix_users_sessions AS users_sessions,
     prefix_users AS users INNER JOIN 
     users_sessions 
     ON users.id = users_sessions.user_id

我的错误:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users_sessions'

我不明白这条消息...我没看到更多使用别名的地方。

谁能帮我解决这个问题?

预先感谢您的帮助。

--已解决--

好的查询如下:

    SELECT users_sessions.id AS users_sessions_id,
           users_sessions.user_id AS users_sessions_user_id,
           users.lastname AS users_lastname, users.firstname AS
           users_firstname
    FROM prefix_users_sessions AS users_sessions
           JOIN prefix_users AS users ON users.id = users_sessions.user_id 

您使用了名称 users_sessions 两次。 首先作为 prefix_users_sessions 的别名,然后作为 INNER JOIN 中的普通 table。还尝试使用 INNER JOINs 而不是同时从两个 tables 中选择,例如(在您的查询中):

FROM prefix_users_sessions AS users_sessions, prefix_users AS users 

您指定了 table prefix_users_sessions 两次

SELECT  users_sessions.user_id AS users_sessions_user_id,
        users.last_name AS users_last_name,
        users.first_name AS users_first_name
FROM    prefix_users_sessions AS users_sessions
        INNER JOIN prefix_users AS users 
            ON users.id = users_sessions.user_id

你的 from 子句一团糟。一个简单的规则:NeverFROM 子句中使用逗号。 始终 使用显式 JOIN 语法。

此外,使用较短的 table 别名,以便您的查询更易于编写和阅读:

SELECT us.user_id AS users_sessions_user_id,
       u.last_name AS users_last_name,
       u.first_name AS users_first_name 
FROM prefix_users_sessions us INNER JOIN
     prefix_users  u 
     ON u.id = us.user_id;

这假设您实际上没有 table 称为 users_sessions,并且打算使用称为 prefix_users_sessions 的 table。