0
SELECT COUNT(wash) AS HandHyCounts, COUNT(enex) AS EnExCounts,
(COUNT(wash)*100/COUNT(enex)) AS Performance, DATE_FORMAT(time, ?) AS groups 
FROM ((SELECT true AS enex, null AS wash, time FROM enexlive
WHERE time >= ? AND time <= ? AND unitid IN (?)) 
UNION ALL 
(SELECT null AS enex, true AS wash, time 
FROM washtablelive WHERE time >= ? AND time <= ? AND unitid IN (?))) AS t1 
GROUP BY groups ORDER BY groups;

MySQL 是否可以为未出现在数据库表中的 Date_Format 返回 0 计数?

示例:假设您将日期格式设置为 %m-%d-%k,因此它显示月-日-小时,并且您正在查找时间戳在 2 月 26 日下午 1 点到 2 月 27 日下午 1 点之间的数据。但在 2 月 27 日凌晨 5 点到 2 月 27 日下午 1 点之间没有数据。

SELECT COUNT(wash) AS HandHyCounts, COUNT(enex) AS EnExCounts,
(COUNT(wash)*100/COUNT(enex)) AS Performance, DATE_FORMAT(time, '%m-%d-%k') AS groups 
FROM ((SELECT true AS enex, null AS wash, time FROM enexlive
WHERE time >= '2013-02-26 13:50:33' AND time <= '2013-02-27 13:50:33' AND unitid IN ('6')) 
UNION ALL 
(SELECT null AS enex, true AS wash, time 
FROM washtablelive WHERE time >= '2013-02-26 13:50:33' AND time <= '2013-02-27 13:50:33' AND unitid IN ('6'))) AS t1 
GROUP BY groups ORDER BY groups;

这就是我得到的:

+--------------+------------+-------------+----------+
| HandHyCounts | EnExCounts | Performance | groups   |
+--------------+------------+-------------+----------+
|           12 |         47 |     25.5319 | 02-26-13 |
|           87 |        268 |     32.4627 | 02-26-14 |
|           80 |        261 |     30.6513 | 02-26-15 |
|           71 |        275 |     25.8182 | 02-26-16 |
|           71 |        270 |     26.2963 | 02-26-17 |
|           58 |        154 |     37.6623 | 02-26-18 |
|           45 |        141 |     31.9149 | 02-26-19 |
|           53 |        281 |     18.8612 | 02-26-20 |
|           44 |        175 |     25.1429 | 02-26-21 |
|           29 |        140 |     20.7143 | 02-26-22 |
|           22 |        107 |     20.5607 | 02-26-23 |
|           26 |        139 |     18.7050 | 02-27-0  |
|           23 |         56 |     41.0714 | 02-27-1  |
|           23 |        154 |     14.9351 | 02-27-2  |
|           18 |        144 |     12.5000 | 02-27-3  |
|           19 |         93 |     20.4301 | 02-27-4  |
+--------------+------------+-------------+----------+

这就是我要的:

+--------------+------------+-------------+----------+
| HandHyCounts | EnExCounts | Performance | groups   |
+--------------+------------+-------------+----------+
|           12 |         47 |     25.5319 | 02-26-13 |
|           87 |        268 |     32.4627 | 02-26-14 |
|           80 |        261 |     30.6513 | 02-26-15 |
|           71 |        275 |     25.8182 | 02-26-16 |
|           71 |        270 |     26.2963 | 02-26-17 |
|           58 |        154 |     37.6623 | 02-26-18 |
|           45 |        141 |     31.9149 | 02-26-19 |
|           53 |        281 |     18.8612 | 02-26-20 |
|           44 |        175 |     25.1429 | 02-26-21 |
|           29 |        140 |     20.7143 | 02-26-22 |
|           22 |        107 |     20.5607 | 02-26-23 |
|           26 |        139 |     18.7050 | 02-27-0  |
|           23 |         56 |     41.0714 | 02-27-1  |
|           23 |        154 |     14.9351 | 02-27-2  |
|           18 |        144 |     12.5000 | 02-27-3  |
|           19 |         93 |     20.4301 | 02-27-4  |
|            0 |          0 |        NULL | 02-27-5  |
|            0 |          0 |        NULL | 02-27-6  |
|            0 |          0 |        NULL | 02-27-7  |
|            0 |          0 |        NULL | 02-27-8  |
|            0 |          0 |        NULL | 02-27-9  |
|            0 |          0 |        NULL | 02-27-10 |
|            0 |          0 |        NULL | 02-27-11 |
|            0 |          0 |        NULL | 02-27-12 |
+--------------+------------+-------------+----------+
4

1 回答 1

1

是的。但这个答案太短了。

count()总是返回一个值,从不NULL。您可以通过一个简单的查询来验证这一点,例如:

select count(*)
from information_schema.columns
where 1 = 0;

这返回0,不是NULL

我很确定这是符合 ANSI 的行为。

另一方面,您有按count()价值划分的部分。在这种情况下,MySQL 将返回NULLfor0/0而不是生成错误(其他数据库可能会这样做)。您也可以轻松地将其模仿为:

select count(table_name) / count(*)
from information_schema.columns
where 1 = 0;

这将返回一行,其中一列的值为NULL

于 2013-03-24T19:48:43.170 回答