选择查询中的未知列语法错误

时间:2018-02-05 14:23:50

标签: mysql sql select join

如何解决此问题:

SQL错误:

Unknown column 'exdays_relation.exercise_id' in 'on clause'

SQL查询:

SELECT * FROM
  `user_days`
LEFT JOIN `menu` ON `menu`.`id` = `exdays_relation`.`exercise_id`
JOIN `exdays_relation` ON `exdays_relation`.`day_id` = `user_days`.`day_id`
WHERE `user_days`.`for_date` LIKE '2016-12-12'

查询关系:

menu
-------
id  int(11) PK  AUTO_INCREMENT 

exdays_relation
-------------------
exdaysrel_id       int(11)  PK  AUTO_INCREMENT
exercise_id        int(11)  FK  from id(menu)   
day_id             int(11)  FK  from day_id(user_days)


user_days
-----------------
day_id      int(11)     PK 
for_date    date

2 个答案:

答案 0 :(得分:2)

因为您JOINuser_daysmenu,但提到exdays_relation

`user_days`
 LEFT JOIN `menu` ON `menu`.`id` = `exdays_relation`.`exercise_id`

您需要提及正在加入的表中的列,您不能在别处提及列名。

答案 1 :(得分:1)

切换连接。第一个连接使用尚未连接的表

SELECT * 
FROM user_days 
JOIN exdays_relation ON exdays_relation.day_id = user_days.day_id 
LEFT JOIN menu ON menu.id = exdays_relation.exercise_id 
WHERE user_days.for_date = '2016-12-12'