非常困惑为什么会这样,因为我将特定表命名为它抱怨的es
和sub
(并且我认为这就是解决特定问题的方式)。
我有:users
submissions_comments
email_settings
submissions
表
该查询提供了我想要的内容,但是现在我想将其加入到我的email_settings
和submissions
表中,它们都具有user_id
。
SELECT user_id,
submission_id,
comment,
users.*,
FROM submissions_comments
INNER JOIN
(SELECT submissions_comments.parent_id
FROM submissions_comments
WHERE id = 224) x ON submissions_comments.id = x.parent_id
LEFT JOIN users ON submissions_comments.user_id = users.id
输出:
user_id: 35
submission_id: 12
comment: fdasadfsdadfs
id: 35
email: [email protected]
username: bobcobb
name: Robert Cobb
about:
created: 2014-03-21 20:24:57
last_login: 2014-07-06 23:21:43
public_profile: 1
queued_photo: 0
但是,如果我尝试加入另一个表(对
user_id
的引用),则会得到Column 'user_id' in field list is ambiguous
SELECT user_id,
submission_id,
comment,
users.*,
es.*,
sub.*
FROM submissions_comments
INNER JOIN
(SELECT submissions_comments.parent_id
FROM submissions_comments
WHERE id = 224) x ON submissions_comments.id = x.parent_id
LEFT JOIN users ON submissions_comments.user_id = users.id
LEFT JOIN submissions sub ON x.parent_id = sub.user_id
LEFT JOIN email_settings es ON x.parent_id = es.user_id;
如您所知,我将所有这些都加入到内部选择(例如
x.parent_id
)返回的user_id上。 最佳答案
您必须将别名添加到列名:
SELECT sub.user_id,...
关于mysql - 字段列表在与4个表连接时含糊不清,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24819250/