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 |
+--------------+------------+-------------+----------+