【问题标题】:Show multiple selection with 3 columns in MYSQL and values NULL在 MYSQL 中显示具有 3 列且值为 NULL 的多项选择
【发布时间】:2014-01-12 15:14:07
【问题描述】:

我有这两张桌子:

学生:

id_pupil
name
surname
email
user
pass
level
class

事件:

id_incidents
date
time
type_incident(miss, delay or attitude)
comments
id_pupil
id_user
subject
id_trimester

我想得到这个:

我写这个:

select pupils.id_pupil, name, surname, count(type_incident) as misses 
from pupils left join incidents using (id_pupil) 
where type_incident='miss' and level=1 and class='B' and id_trimester=1 
group by id_pupil

我得到了每个学生未命中的列。但是,我没有得到空值,因为我还想得到 0 次未命中的学生。

延迟和态度也是如此:

select pupils.id_pupil, name, surname, count(type_incident) as misses 
from pupils left join incidents using (id_pupil) 
where type_incident='delay' and level=1 and class='B' and id_trimester=1 
group by id_pupil

select pupils.id_pupil, name, surname, count(type_incident) as misses 
from pupils left join incidents using (id_pupil) 
where type_incident='attitude' and level=1 and class='B' and id_trimester=1 
group by id_pupil

我想把所有东西都放在图片上描述的同一张桌子上。

谢谢!

【问题讨论】:

    标签: php mysql join count row


    【解决方案1】:

    我认为 juergen 的答案会比我的更好,因为代码更具可读性,但由于我已经在输入我的答案,我将继续发布另一种方法来达到相同的结果:我最初的两个建议是添加 @ 987654321@ 和 surname 到您的 GROUP BY 语句;此外,将条件 and id_trimester=1 从 WHERE 移到您的 LEFT JOIN 中,如下所示:

    select p.id_pupil, name, surname, count(i.type_incident) as misses, count(i2.type_incident) as delays, count(i3.type_incident) as attitudes
    from pupils p
    left join incidents i on i.id_pupil = p.id and i.type_incident='miss' and i.id_trimester=1
    left join incidents i2 on i2.id_pupil = p.id and i2.type_incident='delay' and i2.id_trimester=1
    left join incidents i3 on i3.id_pupil = p.id and i3.type_incident='attitude' and i3.id_trimester=1
    where p.level=1 and p.class='B'
    group by p.id_pupil, name, surname
    

    【讨论】:

      【解决方案2】:

      将条件放入sum()。它总结了您的条件成立的次数(1

      select pupils.id_pupil, name, surname, 
             sum(type_incident='miss' and level=1 and class='B' and id_trimester=1) as misses,
             sum(type_incident='delay' and level=1 and class='B' and id_trimester=1) as delays,
             sum(type_incident='attitude' and level=1 and class='B' and id_trimester=1) as attitudes
      from pupils 
      left join incidents using (id_pupil) 
      group by id_pupil, name, surname
      

      SQLFiddle demo

      【讨论】:

      • 所有字段都返回0 :(
      • 我写了这个,但它显示相同:选择学生 ID_瞳孔,姓名,姓氏,总和(当 type_incident='miss' and level=1 and class='B' and id_trimester=1 then 1 时的情况else 0 end) 作为未命中,sum(当 type_incident='delay' and level=1 and class='B' and id_trimester=1 then 1 else 0 end) as delays, sum(case when type_incident='attitude' and level =1 和 class='B' 和 id_trimester=1 然后 1 否则 0 结束)作为学生离开的态度加入事件使用(id_pupil)按 id_pupil、姓名、姓氏分组
      • 但是,它不起作用......我不明白问题出在哪里
      • 是的,你是对的,它运行良好。另一个问题?我怎样才能得到 0 而不是 NULL?
      • 不确定您的意思,但您可以随时使用案例:when some_column is null then 0 else some_column end
      【解决方案3】:
      SELECT CONCAT(p.name, ' ', p.surname) AS Pupil, t1.misses, t2.delays, t3.attitude
      FROM pupils AS p
      LEFT JOIN (SELECT COUNT(*) AS misses FROM id_incidents WHERE type_incident = 'miss' and level=1 and class='B' and id_trimester=1) AS t1 ON (t1.id_user = p.id_pupil)
      LEFT JOIN (SELECT COUNT(*) AS delays FROM id_incidents WHERE type_incident = 'delay' and level=1 and class='B' and id_trimester=1) AS t2 ON (t2.id_user = p.id_pupil)
      LEFT JOIN (SELECT COUNT(*) AS attitude FROM id_incidents WHERE type_incident = 'attitude' and level=1 and class='B' and id_trimester=1) AS t3 ON (t3.id_user = p.id_pupil)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-25
        相关资源
        最近更新 更多