php - MYSQL用左连接连接3个表

标签 php mysql sql join left-join

我有这些表:

pupils: (id_pupil, name, surname, email, user, pass, level, class)
incidents: (id_incidents, date, time, type_incident, comments, id_pupil, id_user, subject, id_trimester)
qualifications: (id_qualification, qualification, date, time, subject, id_pupil, id_user, id_trimester, type_qualification)

我做了这个查询:

SELECT 
    pupils.id_pupil
  , name
  , surname
  , round(avg(qualifications.calificacion),2) as average
  , count(qualifications.calificacion) as count
  , COALESCE(sum(type_incident='miss' and level=1 and class='A' and id_trimester=1),0) as misses
  , COALESCE(sum(type_incident='delay' and level=1 and class='A' and id_trimester=1),0) as delays
  , COALESCE(sum(type_incident='attitude' and level=1 and class='A' and id_trimester=1),0) as attitude
  , COALESCE(sum(type_incident='miss_justif' and level=1 and class='A' and id_trimester=1),0) as misses_justificadas 
FROM 
    pupils 
    left join incidents         ON incidents.id_pupil=pupils.id_pupil
    left join qualifications    ON qualifications.id_pupil=pupils.id_pupil
WHERE 
    level=1
    and class='A' 
    and id_trimester=1 
    and type_qualification='class' 
    and qualifications.id_trimester=incidents.id_trimester
GROUP BY id_pupil

但问题是,我必须得到的正确结果是:

enter image description here

我得到:

enter image description here

问题在于,将类(class)资格和错过的次数相乘(17x6(2次错过,1次延迟,1种态度和2次错过_just)= 102)),我不希望这样。我想要第一张图片中显示的正确信息。

请问你愿意帮助我吗?

谢谢!

最佳答案

问题是您正在创建的关系复制了很多行。

我个人的做法是:

  1. 首先从事件获取所需的数据
  2. 将“子查询”加入您需要的学生和资格数据

类似这样的事情:

select 
    p.id_pupil, p.name, p.surname, 
    (case
        when type_incident = 'miss' and level = 1 and class='A' then incident_count 
        else 0
    end) as misses,
    (case
        when type_incident = 'delay' and level = 1 and class='A' then incident_count 
        else 0
    end) as delays,
    (case
        when type_incident = 'attitude' and level = 1 and class='A' then incident_count 
        else 0
    end) as attitude,
    (case
        when type_incident = 'miss_justif' and level = 1 and class='A' then incident_count 
        else 0
    end) as miss_justif
from 
    pupils as p
    left join (
        select id_pupil, class, id_trimester, type_qualification, avg(qualification) as q_avg
        from qualifications
        group by id_pupil, class, id_trimester, type_qualification
    ) as q on p.id_pupil = q.id_pupil
    left join (
        select id_pupil, class, type_incident, level, class, count(id_incidents) as incident_count
        from incidents
        group by id_pupil, id_trimester, incident, level, class
    ) as i on p.id_pupil = i.id_pupil
where ...

关于php - MYSQL用左连接连接3个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21145708/

相关文章:

php - 使用多种自然语言的最有效方法

php - PHP 中无限循环的成语?

c# - 如何在 Microsoft Visual Studio 2012 中使用本地数据库文件添加/编辑/检索数据

php - Shell 脚本命令 "ldap_search"无法与 php exec 或 shell_exec 命令一起使用

mysql - 忽略 MySQL 查询中锁定的行

php - 执行查询时,我在 wampserver MySql 5.7.14 中收到与 sql_mode=only_full_group_by 相关的错误

php - 逆 PHP mysql_fetch_array

php - 如何获取每个用户的所有帖子?

java - 使用 Java 8 将日期作为 Date 对象插入数据库中?

apache - Apache2 的 FPM 不工作(权限被拒绝)