#1054 - “on 子句”中的未知列
Posted
技术标签:
【中文标题】#1054 - “on 子句”中的未知列【英文标题】:#1054 - Unknown column in 'on clause' 【发布时间】:2013-11-20 20:44:19 【问题描述】:我收到此错误:
“examinationresults.userid”
我看过其他关于此的帖子,但我似乎无法弄清楚。我不认为我指的是任何不正常的事情......
SELECT examinationresults.id AS id, examinationresults.examid AS examid,
examination.tag AS examtag, examination.title AS examtitle, examinationresults.revision AS revision,
examination.medicalspecialtyid AS medicalspecialtyid, medicalspecialties.title AS medicalspecialtytitle,
examination.institutionid AS institutionid, institutions.title AS institutiontitle,
examinationresults.grade AS grade, examinationresults.points AS points, examinationresults.pointsof AS pointsof,
examinationresults.datebegan AS datebegan, examinationresults.datefinished AS datefinished,
examinationresults.totaltime AS totaltime, examinationresults.finished AS finished, examinationresults.graded AS graded,
users.username AS username, users.firstname AS firstname, users.middlename AS middlename, users.lastname AS lastname
FROM examinationresults, examination
INNER JOIN medicalspecialties
ON examination.medicalspecialtyid=medicalspecialties.id
INNER JOIN institutions
ON examination.institutionid=institutions.id
INNER JOIN users
ON examinationresults.userid=users.id
WHERE examination.examid=examinationresults.examid AND examination.revision=examinationresults.revision AND examinationresults.userid='1'
ORDER BY examinationresults.datefinished ASC;
【问题讨论】:
【参考方案1】:你有
FROM examinationresults, examination
INNER JOIN medicalspecialties /*....*/
不要混合使用 ANSI 89 和 ANSI 92 连接样式。
对examinationresults
和examination
也使用INNER JOIN
语法。
FROM examinationresults
INNER JOIN examination
ON examination.examid = examinationresults.examid
AND examination.revision = examinationresults.revision
INNER JOIN medicalspecialties
ON examination.medicalspecialtyid = medicalspecialties.id
INNER JOIN institutions
ON examination.institutionid = institutions.id
INNER JOIN users
ON examinationresults.userid = users.id
WHERE examinationresults.userid = '1'
【讨论】:
完美。这更有意义......它确实是通过使用 FROM/WHERE 加入以两种不同的方式做同样的事情...... INNER JOIN(我假设是 ANSI 92?)更好更清晰。 @Edward - 是的,内部连接是 ANSI 92 版本。如果您混合连接类型,您最终也会混淆优先级。我只是在谷歌搜索优先级问题时注意到这实际上是一个骗局。 More explanation in the answer here【参考方案2】:您忘记对 examinationresults
和 examination
使用 INNER JOIN 语法。试试这个:
SELECT examinationresults.id AS id, examinationresults.examid AS examid,
examination.tag AS examtag, examination.title AS examtitle, examinationresults.revision AS revision,
examination.medicalspecialtyid AS medicalspecialtyid, medicalspecialties.title AS medicalspecialtytitle,
examination.institutionid AS institutionid, institutions.title AS institutiontitle,
examinationresults.grade AS grade, examinationresults.points AS points, examinationresults.pointsof AS pointsof,
examinationresults.datebegan AS datebegan, examinationresults.datefinished AS datefinished,
examinationresults.totaltime AS totaltime, examinationresults.finished AS finished, examinationresults.graded AS graded,
users.username AS username, users.firstname AS firstname, users.middlename AS middlename, users.lastname AS lastname
FROM examinationresults
INNER JOIN examination
ON examination.examid = examinationresults.examid
AND examination.revision = examinationresults.revision
INNER JOIN medicalspecialties
ON examination.medicalspecialtyid = medicalspecialties.id
INNER JOIN institutions
ON examination.institutionid = institutions.id
INNER JOIN users
ON examinationresults.userid = users.id
WHERE examinationresults.userid = '1'
附带说明:
你可以检查
Bad habits to kick : using old-style JOINs
供 Aaron Bertrand 参考。【讨论】:
以上是关于#1054 - “on 子句”中的未知列的主要内容,如果未能解决你的问题,请参考以下文章
未找到列:1054“on 子句”中的未知列“comments.commenter_id”
未找到列:Laravel 5 多对多关系查询中“on 子句”中的 1054 未知列“managers.id”
MySQL错误1054未知列'persons.PersonID' in on子句[重复]
未找到列:1054“where 子句”中的未知列“MONTH(date)”[重复]
“java.sql.SQLSyntaxErrorException:'on 子句'中的未知列'*'”在休眠条件下加入表时