mysql获取组数

bmp9r5qi  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(281)

我有一个简单的mysql,它根据where和having子句返回分组数据。

SELECT Platform, `Test Procedure`, `Order Number` 
FROM Stage 
WHERE (`StartTime` BETWEEN "2018-4-23" AND DATE_ADD("2018-4-24",INTERVAL 1 DAY)) 
GROUP BY `Order Number` 
HAVING (Stage.Platform =  "Kingston") AND (Stage.`Test Procedure` = "Calibration and test")

我需要得到组数,而不是每个组的记录数。
在sql语句中是否有可能,或者我最好尝试通过aspx.cs端访问它?

lo8azlld

lo8azlld1#

与另一个获得所需计数的查询联接。

SELECT Platform, `Test Procedure`, `Order Number`, `Order Count`
FROM Stage 
CROSS JOIN (
    SELECT COUNT(DISTINCT `Order Number`) AS `Order Count`
    FROM Stage
    WHERE `StartTime` BETWEEN "2018-4-23" AND DATE_ADD("2018-4-24",INTERVAL 1 DAY)
    AND Stage.Platform =  "Kingston" AND Stage.`Test Procedure` = "Calibration and test"
) AS x
WHERE (`StartTime` BETWEEN "2018-4-23" AND DATE_ADD("2018-4-24",INTERVAL 1 DAY)) 
AND (Stage.Platform =  "Kingston") AND (Stage.`Test Procedure` = "Calibration and test")
GROUP BY `Order Number`

似乎也没有任何理由 Platform 以及 Test Procedure 测试 HAVING 而不是 WHERE . HAVING 应用于在中计算的列 SELECT 列表,而不是来自表行的值。

相关问题