on子句中的“ id_training”列不明确

时间:2018-12-17 03:59:54

标签: mysql sql

我想显示course_title数据,但得到:

  on子句中的

'id_training'不明确。

查询中的错误在哪里?

CREATE TABLE `examination` (
`id_exam` int(32) NOT NULL,  
`class_name` varchar(100) NOT NULL,
`id_training` int(32) NOT NULL, 
`id_question` int(32) NOT NULL,
`total` int(3) NOT NULL,  
`exam_duration` int(5) NOT NULL,
`start` datetime NOT NULL,
`end` datetime NOT NULL,
`passing_grade` int(3) NOT NULL,
`id_user` int(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `training` (
`id_training` int(32) NOT NULL,  
`course_code` varchar(25) NOT NULL,
`course_title` varchar(100) NOT NULL,
`training_time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

查询:

SELECT e.id_exam
     , e.class_name
     , e.id_training
     , e.total
     , e.exam_duration
     , e.start_exam
     , e.end_exam
     , e.passing_grade
     , t.course_title 
FROM examination e
INNER JOIN training t
ON id_training = course_tilte

4 个答案:

答案 0 :(得分:2)

此错误的原因“ on子句中的列'id_training'不明确” 是相同的列,并且在两个表中都存在相同的列,因此由于指定了 sql的where子句中的id_training列与我应该从哪个表中混淆,因此请始终使用别名进行指定。

SELECT e.id_exam
     , e.class_name
     , e.id_training
     , e.total
     , e.exam_duration
     , e.start_exam
     , e.end_exam
     , e.passing_grade
     , t.course_title 
  FROM examination e
  JOIN training t
    ON e.id_training = t.id_training

答案 1 :(得分:0)

SELECT e.id_exam, e.class_name, e.id_training, 
e.total, e.exam_duration, e.start_exam, 
e.end_exam, e.passing_grade, t.course_title 
FROM examination e
INNER JOIN training t  
ON e.id_training = t.course_title

答案 2 :(得分:0)

您的查询看起来不正确。应该是这样的:

SELECT examination.id_exam, examination.class_name, examination.id_training, examination.total, examination.exam_duration, examination.start_exam, examination.end_exam, examination.passing_grade, training.course_title 
FROM examination 
INNER JOIN training 
ON training.id_training = examination.id_training

答案 3 :(得分:0)

请尝试以下操作:

ON t.id_training = e.id_training