如何在SQL中加入四个表..?

时间:2019-01-07 06:42:20

标签: php sql mariadb

我编写了sql查询,其中我将四个表与student_id联接在一起在所有四个表中都很常见。请告诉我我的sql查询中有什么错误。我得到此错误。

  

您的SQL语法有错误;请查看与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第1行“ COUNT(CASE WHEN account.status ='pending'THEN 1 ELSE NULL END)AS'pending','附近使用

SELECT student_branch.level_id COUNT(CASE WHEN account.status = 'pending' THEN 1 ELSE NULL END) AS 'pending',
        COUNT(CASE WHEN account.status = 'paid' THEN 1 ELSE NULL END) AS 'paid',
        COUNT(*) AS total FROM student LEFT JOIN account ON student.student_id=account.student_id LEFT JOIN student_branch ON student.student_id=student_branch.student_id WHERE student_branch.level_id!=''  AND student.status = 'ACTIVE'
               AND student_branch.branch_id = '$data' AND account.fee_active = 1 GROUP BY student_branch.level_id

2 个答案:

答案 0 :(得分:0)

尝试以下操作-您错过了逗号

select  c.delivery_id
,count(c.carton_id) as count_of_carton
,count(ds.carton_id) as count_of_deliveries_shipped
from carton as c 
left join deliveries_shipped as ds on c.delivery_id=ds.delivery_id 

group by c.delivery_id 
having count_of_carton<>count_of_deliveries_shipped

答案 1 :(得分:0)

查询正确,但在 student_branch.level_id

之后出现逗号问题
SELECT
student_branch.level_id, COUNT(
    CASE WHEN ACCOUNT.status = 'pending' THEN 1 ELSE NULL
END
) AS 'pending',
COUNT(
CASE WHEN ACCOUNT.status = 'paid' THEN 1 ELSE NULL
END
) AS 'paid',
COUNT(*) AS total
FROM
student
LEFT JOIN ACCOUNT ON student.student_id = ACCOUNT.student_id
LEFT JOIN student_branch ON student.student_id = student_branch.student_id
WHERE
student_branch.level_id != '' AND student.status = 'ACTIVE' AND student_branch.branch_id = '$data' AND ACCOUNT.fee_active = 1
GROUP BY
student_branch.level_id