我使用连接进行简单查询时出错。
我的查询:
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不唯一 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
答案 0 :(得分:1)
你的Rmks
条款全部搞砸了。一个简单的规则:从不在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
的表格。
答案 1 :(得分:0)
您使用名称users_sessions
两次。
首先是prefix_users_sessions
的别名,然后是INNER JOIN
中的普通表。也尝试使用INNER JOIN
s而不是同时从两个表中选择,例如(在您的查询中):
FROM prefix_users_sessions AS users_sessions, prefix_users AS users
答案 2 :(得分: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