Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#1054 - Unknown column in 'on clause'

Tags:

sql

join

mysql

I am getting this error:

#1054 - Unknown column 'examinationresults.userid' in 'on clause'

I've seen other posts on this but I can't seem to figure it out. I don't think I'm referring to anything out of order...

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;
like image 392
Edward Avatar asked Nov 20 '13 20:11

Edward


2 Answers

You have

FROM examinationresults, examination 
INNER JOIN medicalspecialties /*....*/

Don't mix ANSI 89 and ANSI 92 join styles.

Use the INNER JOIN syntax for examinationresults and examination too.

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'
like image 134
Martin Smith Avatar answered Sep 22 '22 09:09

Martin Smith


You forget to use the INNER JOIN syntax for examinationresults and examination .Try this:

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'

On a side note:

You may check

Bad habits to kick : using old-style JOINs

by Aaron Bertrand for reference.
like image 32
Rahul Tripathi Avatar answered Sep 18 '22 09:09

Rahul Tripathi