多个表上具有多个JOINS的SQL查询

时间:2017-06-05 20:27:16

标签: mysql

我已创建此查询以匹配多个表的数据,但我觉得它有点复杂。

我需要从下表中选择:

  • lead
  • instructors_options
  • instructor_calendar

使用以下关系:

leads.sequence = instructors_options.instructor_seq
leads.sequence = instructor_calendar.instructor_seq

以下过滤器必须匹配:

  • lead.lead_type ='讲师'
  • instructors_options.option_name ='pupil_cap'AND instructors_options.value> 0
  • instructors_options.option_name ='car_type'AND instructors_options.value ='variable'
  • instructors_options.option_name ='areas_covered'AND instructors_options.value ='variable'
  • instructor_calendar.pupil_name ='AVAILABLE'AND instructor_calendar.date> ='variable'AND instructor_calendar.date< ='variable'
  • instructors_options.option_name ='diary_updates'AND instructors_options.value ='1'

      SELECT    i.sequence as instructor_seq, 
                    ic.date AS date, 
                    ic.start_time AS start_time, 
                    ic.end_time AS end_time
            FROM 
                leads i 
    
            LEFT JOIN 
                instructors_options io 
            ON 
                i.sequence = io.instructor_seq AND 
                (io.option_name = 'pupil_cap' AND io.value > '0') 
    
            RIGHT JOIN 
                instructors_options io2 
            ON 
                i.sequence = io2.instructor_seq AND 
                io2.option_name = 'car_type' AND io2.value = '".$bookingData["car_type"]."' 
    
            RIGHT JOIN 
                instructors_options io3 
            ON 
                i.sequence = io3.instructor_seq AND 
                io3.option_name = 'areas_covered' AND (io3.value LIKE '".substr($postcode, 0, 1)."' OR io3.value LIKE '".substr($postcode, 0, 2)."' OR io3.value LIKE '".substr($postcode, 0, 3)."' OR io3.value LIKE '".substr($postcode, 0, 4)."')
    
            RIGHT JOIN 
                instructors_options io4 
            ON 
                i.sequence = io4.instructor_seq AND 
                io4.option_name = 'diary_updates' AND io4.value = '1' 
    
            RIGHT JOIN 
                instructor_calendar ic 
            ON 
                i.sequence = ic.instructor_seq AND 
                ic.pupil_name = 'AVAILABLE' AND 
                ic.date >= '".ChangeDateFormat($date, 'Y-m-d')."' AND 
                ic.date <= '".date('Y-m-d', strtotime($date. ' + 7 days'))."' 
            WHERE 
                i.lead_type = 'Instructor' 
            GROUP BY date 
            ORDER BY date ASC, start_time ASC
    

有人可以帮我更新我的查询以确保我已正确完成

谢谢!

1 个答案:

答案 0 :(得分:2)

您正在寻找某些日历条目。所以从那张桌子中选择。

其中一个条件是相关讲师序列具有所有四个选项。因此,聚合每个教师序列的选项并保留符合所有条件的人。使用IN子句获取这些教师序列的日历条目。

select 
  instructor_seq,
  date, 
  start_time, 
  end_time
from instructor_calendar 
where pupil_name = 'AVAILABLE' 
and date >= '2017-01-01' 
and date <= '2017-05-31'
and instructor_seq in
(
  select instructor_seq
  from instructors_options
  group by instructor_seq
  having sum(option_name = 'pupil_cap'     and value > 0)   > 0
     and sum(option_name = 'car_type'      and value = 123) > 0
     and sum(option_name = 'areas_covered' and value = 456) > 0
     and sum(option_name = 'diary_updates' and value = 1)   > 0
);

顺便说一下,HAVING子句使用了MySQL true = 1false = 0