简体   繁体   中英

Unknown column syntax error in select query

How to fix this issue:

SQL Error:

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

SQL query:

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'

query relation:

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

Because, you are JOIN ing with user_days and menu table, but mentioned column from exdays_relation

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

You are required to mention columns from tables, who are getting join, you can not mentioned column names elsewhere.

Switch the joins. The first join uses a tables that has not been joined yet

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM