考虑两个表:USERS
和TASKS
用于任务交付应用
USERS
userID | email | name
TASKS
userID | designator_userUNIQUE | destination_userUNIQUE
要从destination_user
表中获取users
全名,我使用以下查询可以正常运行:
SELECT *
FROM tasks
INNER JOIN users ON users.userID = tasks.destination_userUNIQUE
WHERE tasks.client='$clientUNIQUE'
ORDER BY tasks.date DESC, tasks.time DESC
但是,如果我想使用以下查询从designator_userUNIQUE
获取全名,则只会生成sql错误(mysql_numrows() expects parameter 1 to be resource
)。
SELECT *
FROM tasks
INNER JOIN users ON users.userID = tasks.destination_userUNIQUE
INNER JOIN users ON users.name = tasks.designator_user UNIQUE
WHERE tasks.client='$clientUNIQUE'
ORDER BY tasks.date DESC, tasks.time DESC
有什么想法吗?
答案 0 :(得分:1)
您需要为您加入的users
表的每个实例提供不同的别名,以防止出现歧义。
SELECT *
FROM tasks AS t
INNER JOIN users AS dest ON dest.userID = t.destination_userUNIQUE
INNER JOIN users AS desig ON desig.userID = t.designator_userUNIQUE
WHERE t.client='$clientUNIQUE'
ORDER BY t.date DESC, t.time DESC