如果表中不存在mysql group,则强制返回null值

ct2axkht  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(226)

希望一切顺利,我确实有一个关于mysql的小问题,因为我在查询方面没有太多经验,所以请帮助我解决下面的查询,我有一个标记表有一列根据下表,我想输出一个组的考试类型和返回空值,如果没有记录在表与组。

subject       examtype     fullmark    secured
SCIENCE       CLS          50          20 
SCIENCE       CLS          50          50
MATHEMATICS   MNT          50          10
MATHEMATICS   MNT          50          35
MATHEMATICS   MNT          100         85
SCIENCE       MCK          100         89 
MATHEMATICS   CMP          100         72
ENGLISH       PST          50          33

我目前的问题如下

SELECT round((SUM(`secured`)/SUM(`fullmark`))*100) as percent,subject,examtype FROM tbl_marks WHERE subject = 'MATHEMATICS' GROUP BY examtype

上面的查询返回下面的结果

percent   subject      examtype
    72        MATHEMATICS  CMP
    65        MATHEMATICS  MNT

我希望我的输出如下

percent   subject      examtype
72        MATHEMATICS  CMP
65        MATHEMATICS  MNT
0         MATHEMATICS  CLS
0         MATHEMATICS  MCK
0         MATHEMATICS  PST

如何查询请帮我,提前谢谢。

qmb5sa22

qmb5sa221#

这个查询创建subject和examtypes的每个组合,然后使用一个右外连接来拉入在核心查询中看不到的组合。

SELECT
  coalesce(round((SUM(secured)/SUM(fullmark))*100), 0) as percent,
  all_subjects_examtypes.subject,
  all_subjects_examtypes.examtype
from
  tbl_marks
  right outer join (
    select distinct
      s.subject,
      e.examtype
    from
      tbl_marks as s
      cross join tbl_marks as e
  ) as all_subjects_examtypes on
    tbl_marks.subject = all_subjects_examtypes.subject and
    tbl_marks.examtype = all_subjects_examtypes.examtype
where
  all_subjects_examtypes.subject = 'MATHEMATICS'
group by
  all_subjects_examtypes.examtype
wvt8vs2t

wvt8vs2t2#

表中没有包含这些检查类型的记录,因此必须创建一个包含这些检查类型记录的记录集。
您可以通过交叉连接具有不同examtypes的不同主题来获得该记录集:

SELECT dsubject.subject, dexamptype.examtype
        FROM (SELECT DISTINCT subject FROM tbl_marks) dsubject,
            (SELECT DISTINCT examptype FROM tbl_marks) dexamptype

现在你有了一个关于每门学科和每种考试类型的记录
您可以在以下查询中使用它:

SELECT
    round((SUM(tbl_marks.`secured`)/SUM(tbl_marks.`fullmark`))*100) as percent
    subject_examtype.subject,
    subject_examtype.examtype   
FROM
    (
        SELECT dsubject.subject, dexamptype.examtype
        FROM (SELECT DISTINCT subject FROM tbl_marks WHERE subject = 'MATHEMATICS') dsubject,
            (SELECT DISTINCT examptype FROM tbl_marks) dexamptype
    ) subject_examtype
    LEFT OUTER JOIN tbl_marks 
        ON subject_examtype.subject = tbl_marks.subject
            AND subject_examptype.examtype = subject.examtype
GROUP BY subject_examtype.subject, subject_examtype.examtype;

因为这只是你想要的一个主题,所以这是一样的:

SELECT
    round((SUM(tbl_marks.`secured`)/SUM(tbl_marks.`fullmark`))*100) as percent
    subject_examtype.subject,
    subject_examtype.examtype   
FROM
    (SELECT DISTINCT 'MATHEMATICS' as subject, examtype FROM tbl_marks) subject_examtype
    LEFT OUTER JOIN tbl_marks 
        ON subject_examtype.subject = tbl_marks.subject
            AND subject_examptype.examtype = subject.examtype
GROUP BY subject_examtype.subject, subject_examtype.examtype;

但是如果您想对更多的主题执行此操作,那么查询的第一个版本就更有意义了。

相关问题