1

使用连接的简单查询出现错误。

我的查询:

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]:语法错误或访问冲突:1066 不是唯一的表/别名:'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 
4

3 回答 3

1

你的from条款全乱了。一个简单的规则: 永远不要FROM子句中使用逗号。 始终使用显式JOIN语法。

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

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;

这假设您实际上并没有一个名为 的表users_sessions,其目的是使用一个名为 的表prefix_users_sessions

于 2015-11-05T20:19:59.633 回答
0

你用users_sessions了两次名字。首先作为 的别名,prefix_users_sessions然后作为INNER JOIN. 还尝试使用INNER JOINs 而不是同时从两个表中进行选择,例如(在您的查询中)。:

FROM prefix_users_sessions AS users_sessions, prefix_users AS users 
于 2015-11-05T20:17:05.673 回答
0

您指定了表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
于 2015-11-05T20:19:28.787 回答