查询以返回带有COUNT(*)

时间:2019-10-12 01:50:19

标签: mysql sql

我已经看到许多类似的问题发布到StackOverflow上,但我尝试应用它们并没有成功。

我想采用一个现有的有效COUNT(*)查询并对其进行更新,以使其也返回以零作为计数值的行。我认为我需要使用LEFT JOIN,但不确定如何使它工作。

我当前的工作查询:

SELECT `VIC_HELMET_BELT`.`id`, `VIC_HELMET_BELT`.`HELMET_BELT` AS 'desc', COUNT(*) AS 'count' FROM `VIC_ACCIDENT`, `VIC_NODE`, `VIC_PERSON`, `VIC_VEHICLE`, `VIC_HELMET_BELT` 
WHERE `VIC_NODE`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO` 
AND `VIC_PERSON`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO` 
AND `VIC_VEHICLE`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO`
AND `VIC_PERSON`.`SEATING_POSITION_Id` = 1
AND `VIC_HELMET_BELT`.`id` = `VIC_PERSON`.`HELMET_BELT_WORN`
GROUP BY `VIC_HELMET_BELT`.`id`

这将返回表:

    id  desc                            count  
------  -----------------------------  --------
     1  Seatbelt worn                    390115
     2  Seatbelt not worn                 10158
     4  Child restraint not worn              1
     5  Seatbelt/restraint not fitted      1573
     6  Helmet worn                       60521
     7  Helmet not worn                    3495
     8  Not appropriate                    3635
     9  Not known                        168617

我希望返回id = 3且计数为0的表:

    id  desc                            count  
------  -----------------------------  --------
     1  Seatbelt worn                    390115
     2  Seatbelt not worn                 10158
     3  Child restraint worn                  0
     4  Child restraint not worn              1
     5  Seatbelt/restraint not fitted      1573
     6  Helmet worn                       60521
     7  Helmet not worn                    3495
     8  Not appropriate                    3635
     9  Not known                        168617

如果查询中不清楚,VIC_HELMET_BELT是一个查找表,它等同于上表的iddesc列。 VIC_PERSONS.HELMET_BELT_WORN包含我正在计算的与VIC_HELMET_BELT.id值相对应的值。

任何对我原始SQL的质量的评论也将受到赞赏。我觉得我从未真正了解过如何充分利用SQL查询。

如果您有兴趣,这是来自澳大利亚维多利亚州的崩溃数据。您可能已经注意到,我只为驱动程序过滤了。我真的希望“不戴儿童约束装置”只是错字!

预先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

你在这里

SELECT `VIC_HELMET_BELT`.`id`, `VIC_HELMET_BELT`.`HELMET_BELT` AS 'desc', 
COUNT(`VIC_VEHICLE`.`ACCIDENT_NO`) AS 'count' 
FROM `VIC_HELMET_BELT`
  LEFT JOIN `VIC_PERSON` ON `VIC_HELMET_BELT`.`id` = `VIC_PERSON`.`HELMET_BELT_WORN` AND `VIC_PERSON`.`SEATING_POSITION_Id` = 1     
  LEFT JOIN `VIC_ACCIDENT` ON `VIC_PERSON`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO`
  LEFT JOIN `VIC_NODE` on VIC_NODE`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO`
  LEFT JOIN `VIC_VEHICLE` on `VIC_VEHICLE`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO`    
GROUP BY `VIC_HELMET_BELT`.`id, `VIC_HELMET_BELT`.`HELMET_BELT`

答案 1 :(得分:0)

在下面尝试此查询,查看是否有ID = 3的计数结果。如果为空,则可以使用IFNULL(COUNT(*),0) AS 'Count'

SELECT `VIC_HELMET_BELT`.`id`, `VIC_HELMET_BELT`.`HELMET_BELT` AS 'desc', 
        COUNT(*) AS 'count' 
FROM `VIC_HELMET_BELT` 
LEFT JOIN  `VIC_PERSON` ON `VIC_HELMET_BELT`.`id` = `VIC_PERSON`.`HELMET_BELT_WORN`
LEFT JOIN  `VIC_ACCIDENT` ON `VIC_PERSON`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO` 
LEFT JOIN  `VIC_NODE` ON `VIC_NODE`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO` 
LEFT JOIN  `VIC_VEHICLE` ON `VIC_VEHICLE`.`ACCIDENT_NO` = `VIC_ACCIDENT`.`ACCIDENT_NO`
WHERE  `VIC_PERSON`.`SEATING_POSITION_Id` = 1
GROUP BY `VIC_HELMET_BELT`.`id`

关于查询的注释,您可以尝试在表上使用别名来缩短查询的时间。例如以下示例:

SELECT v1.`id`, v1.`HELMET_BELT` AS 'desc', COUNT(*) AS 'count' 
FROM `VIC_HELMET_BELT` as v1
LEFT JOIN  `VIC_PERSON` as v2 ON v1.`id` = v2.`HELMET_BELT_WORN`
LEFT JOIN  `VIC_ACCIDENT` as v3 ON v2.`ACCIDENT_NO` = v3.`ACCIDENT_NO` 
LEFT JOIN  `VIC_NODE` as v4 ON v2.`ACCIDENT_NO` = v4.`ACCIDENT_NO` 
LEFT JOIN  `VIC_VEHICLE` as v5 ON v2.`ACCIDENT_NO` = v4`.`ACCIDENT_NO`
WHERE  v2.`SEATING_POSITION_Id` = 1
GROUP BY v1.`id`;

每个表都分配有v1, v2, v3 ...,依此类推,这样当您要使用表中的列时,无需键入完整的表名,而只需键入别名

**编辑:**另一种方法是这样的,假设您需要对VIC_HELMET_BELT以外的其他表执行其他操作;只是成为参考表。

SELECT v1.`id`, v1.`HELMET_BELT` AS 'desc', COUNT(*) AS 'count' 
FROM `VIC_HELMET_BELT` AS v1
LEFT JOIN  
(SELECT * FROM `VIC_PERSON` AS v2
INNER JOIN `VIC_ACCIDENT` AS v3 ON v2.`ACCIDENT_NO` = v3.`ACCIDENT_NO` 
INNER JOIN `VIC_NODE` AS v4 ON v2.`ACCIDENT_NO` = v4.`ACCIDENT_NO` 
INNER JOIN `VIC_VEHICLE` AS v5 ON v2.`ACCIDENT_NO` = v4.`ACCIDENT_NO`
WHERE v2.`SEATING_POSITION_Id` = 1) A
ON v1.`id`=A.`HELMET_BELT_WORN`
GROUP BY v1.`id`;