简体   繁体   English

mysql连接无法按预期工作

[英]mysql join not working as expected

i want to select schedule of specific teacher for a given day .... statement should assume that a teacher may teach in elementary and high school 我想选择给定日期的特定老师的时间表..声明应假定老师可以在小学和中学任教

here is my statement 这是我的声明

 `select 
   class.name as subject_classmodel_name, 
   teachers.name as subject_teachers_name, 
   section.name as subject_section_name, 
   day.name as day_name,
   time_format(time_range.time_start,'%H:%i') as time_time_start,
   time_format(time_range.time_end,'%H:%i') as time_time_end,
   sched.subject_ref, sched.time_ref, sched.day_ref
   from schedule sched
   join class_teachers_section
   join time_range
   join day
   join class
   join teachers
   join section
   where teachers.name = "ronald manlapao" and 
   section.level = "elementary" and 
   time_range.level = "elementary" and 
   class_teachers_section.id = sched.subject_ref and 
   class.id = class_teachers_section.class_ref  and 
   teachers.id = class_teachers_section.teachers_ref and 
   section.id = class_teachers_section.section_ref and
   day.id = sched.day_ref
   order by time_range.time_start asc`

class_teachers_section (table) has tables class, teachers, section schedule (table) has tables class_teachers_section, day, time_range class_teachers_section(表)具有表class,teachers,section schedule(表)具有表class_teachers_section,day,time_range

output 输出

在此处输入图片说明

expected result 预期结果

only one row (last row) with time_start (14:00) time_end (15:00) should have data in columns subject_classmodel_name, subject_teachers_name, subject_section_name, subject_ref, time_ref and day_ref. time_start(14:00)time_end(15:00)中只有一行(最后一行)应该在subject_classmodel_name,subject_teachers_name,subject_section_name,subject_ref,time_ref和day_ref列中具有数据。 the rest should be null 其余的应该为空

Im using MYSQL 5.6. 我正在使用MYSQL 5.6。

I think it's not working because of you are missing "ON" statement 我认为它不起作用,因为您缺少“ ON”语句

the right syntax is 正确的语法是

SELECT field FROM tableA
JOIN tableB
ON tableA.id = tableB.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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