' on子句'中的未知列错误

时间:2018-01-12 22:11:28

标签: mysql sql

所以我试图查询三个表,可以做三个中的两个但不是第三个,我已经尝试了几种方法并继续收到此错误:

#1054 - Unknown column 'thirdDATA.creator_id' in 'on clause'

这有效:

SELECT * 
FROM `mainDATA` 
LEFT JOIN altDATA ON creator_id =  altDATA.creator_id

这有效:

SELECT * 
FROM `mainDATA` 
LEFT JOIN thirdDATA ON creator_id =  thirdDATA.creator_id
LIMIT 0 , 30

这会出错:

SELECT * 
FROM `mainDATA` 
LEFT JOIN altDATA ON creator_id =  altDATA.creator_id
JOIN thirdDATA ON creator_id =  thirdDATA.creator_id

2 个答案:

答案 0 :(得分:3)

如果查询中有多个表,请限定所有列名称。

据推测,你打算:

SELECT * 
FROM mainDATA LEFT JOIN
     altDATA
     ON mainDATA.creator_id =  altDATA.creator_id JOIN
     thirdDATA
     ON mainDATA.creator_id =  thirdDATA.creator_id;

如果mainDATA没有creator_id,您描述的行为就会奏效。如果是这种情况,第一个查询将被解释为:

SELECT * 
FROM `mainDATA` LEFT JOIN
     altDATA
     ON altDATA.creator_id =  altDATA.creator_id;

第三个会产生错误,因为两个表都有creator_id,并且不清楚最后一个on子句指的是哪个。

答案 1 :(得分:0)

混合JOINLEFT JOIN时,请使用括号覆盖优先顺序:

SELECT * 
FROM ( `mainDATA` 
       LEFT JOIN altDATA ON creator_id =  altDATA.creator_id )
JOIN thirdDATA ON creator_id =  thirdDATA.creator_id

SELECT * 
FROM `mainDATA` 
LEFT JOIN ( altDATA
            JOIN thirdDATA ON creator_id =  thirdDATA.creator_id )
   ON creator_id =  altDATA.creator_id

这样做可以解决您的问题,也可以指出creator_id无法访问,因为它隐藏在parens中。