MySQL语法错误[Left Join]

时间:2017-08-14 08:47:08

标签: mysql join left-join

所以我在下面有这个查询,它给了我一个语法错误。我将在查询下方显示错误。

请查看以下内容:

SELECT g.fullname, g.id as guardian_id, g.email, g.email_2, s.title, s.domain, d.device_id as device, d.os, gf.booking_open_notified, gf.days_notified_7, gf.days_notified_3, gf.day_notified, s.id as school_id, p.firstname, p.surname 
FROM guardian as g
LEFT JOIN guardian_form as gf ON gf.school_id = g.school_id
LEFT JOIN school as s ON s.id = g.school_id 
LEFT JOIN school_design as sd ON sd.school_id = s.id 
LEFT JOIN device as d ON d.school_id = s.id AND d.guardian_id = g.id 
    (
        LEFT JOIN pupil as p ON p.school_id = s.id AND 
        (
            p.grade_id = gf.grade_id
            OR p.grade_id = gf.grade_id_2
            OR p.grade_id = gf.grade_id_3
            OR p.grade_id = gf.grade_id_4
            OR p.grade_id = gf.grade_id_5
            OR p.grade_id = gf.grade_id_6
            OR p.grade_id = gf.grade_id_7
            OR p.grade_id = gf.grade_id_8
            OR p.grade_id = gf.grade_id_9
            OR p.grade_id = gf.grade_id_10
        )
    ) OR (
        LEFT JOIN guardian_form_group as gfg ON gfg.guardian_form_id = gf.id
        LEFT JOIN pupil_group as pg ON pg.group_id = gfg.group_id
        LEFT JOIN pupil as p ON p.id = pg.pupil_id AND pg.school_id = s.id
    )
WHERE (
    gf.booking_open_notified != 1 
    OR gf.days_notified_7 != 1 
    OR gf.days_notified_3 != 1 
    OR gf.day_notified != 1 
)
AND gf.active = 1
AND sd.disable_parents_evening_notification_and_email = 0 
GROUP BY gf.id, p.id 
LIMIT 20

错误:

  

错误SQL查询:文档

SELECT g.fullname, g.id as guardian_id, g.email, g.email_2, s.title,
s.domain, d.device_id as device, d.os, gf.booking_open_notified,
gf.days_notified_7, gf.days_notified_3, gf.day_notified, s.id as
school_id, p.firstname, p.surname  FROM guardian as g LEFT JOIN
guardian_form as gf ON gf.school_id = g.school_id LEFT JOIN school as
s ON s.id = g.school_id  LEFT JOIN school_design as sd ON sd.school_id
= s.id  LEFT JOIN device as d ON d.school_id = s.id AND d.guardian_id = g.id      
(
  LEFT JOIN pupil as p ON p.school_id = s.id AND 
  (
        p.grade_id = gf.grade_id
        OR p.grade_id = gf.grade_id_2
        OR p.grade_id = gf.grade_id_3
        OR p.grade_id = gf.grade_id_4
        OR p.grade_id = gf.grade_id_5
        OR p.grade_id = gf.grade_id_6
        OR p.grade_id = gf.grade_id_7
        OR p.grade_id = gf.grade_id_8
        OR p.grade_id = gf.grade_id_9
        OR p.grade_id = gf.grade_id_10
  )
) OR 
     

MySQL说:文档

     

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   

附近
'JOIN pupil as p ON p.school_id = s.id AND 
    (
        p.grade_id = gf.' at line 8

1 个答案:

答案 0 :(得分:0)

您的语法就像

SELECT listOfColumns FROM table1 LEFT JOIN table2 (LEFT JOIN table3) OR (LEFT JOIN table4)

为什么要尝试使用'或'加入?这不受支持。如果要过滤掉表格,请尝试改善ON条件