我有一个mysql查询,它获取0或300以上的行总数,并且它工作正常,除了我需要查询将一些行组合在一起。
这是表格:
+------------------------------+---------------------+------------+
| color | check_date | color_code |
+------------------------------+---------------------+------------+
| red | 2017-06-20 13:52:18 | 408 |
| green | 2017-06-17 19:10:25 | 524 |
| green | 2017-06-19 12:17:31 | 314 |
| light-red | 2017-06-19 10:51:05 | 227 |
| dark-green | 2017-06-25 10:47:50 | 82 |
| dark-blue | 2017-06-21 09:23:43 | 0 |
| light-blue | 2017-06-20 09:24:50 | 900 |
| yellow | 2017-06-19 11:01:46 | 833 |
| medium-yellow | 2017-06-07 16:07:49 | 0 |
| medium-red | 2017-06-16 09:46:38 | 19 |
| dark-green | 2017-06-21 08:47:50 | 822 |
| dark-blue | 2017-06-01 15:23:43 | 12 |
| light-blue | 2017-06-22 11:24:50 | 900 |
| yellow | 2017-06-21 09:23:43 | 8 |
| medium-yellow | 2017-06-17 14:07:49 | 11 |
| light-blue | 2017-06-16 09:46:38 | 19 |
+------------------------------+---------------------+------------+
以下是查询:
SELECT color,
max(check_date) AS check_date,
count(*) AS total,
sum( color_code < 1 OR color_code >= 300) AS cnt
FROM check_colors
GROUP BY color
ORDER BY cnt DESC LIMIT 10;
这将返回如下表格:
+------------------------------+---------------------+-------+------+
| color | check_date | total | cnt |
+------------------------------+---------------------+-------+------+
| light-blue | 2017-06-22 11:24:50 | 3 | 2 |
| green | 2017-06-19 12:17:31 | 2 | 2 |
| dark-green | 2017-06-25 10:47:50 | 2 | 1 |
| medium-yellow | 2017-06-17 14:07:49 | 2 | 1 |
| dark-blue | 2017-06-21 08:47:50 | 2 | 1 |
| yellow | 2017-06-21 09:23:43 | 2 | 1 |
| red | 2017-06-20 13:52:18 | 1 | 1 |
| light-red | 2017-06-19 10:51:05 | 1 | 0 |
| medium-red | 2017-06-16 09:46:38 | 1 | 0 |
+------------------------------+---------------------+-------+------+
我需要的是一个mysql查询,它将浅色,深色和中等前缀与未加前缀的颜色组合在一起。例如,不是将绿色和深绿色作为单独的行,而是将它们组合在一起,而颜色列将显示为绿色。结果将是总数:4,cnt:3。
我需要上面查询中的reults表看起来像这样:
+------------------------------+---------------------+-------+------+
| color | check_date | total | cnt |
+------------------------------+---------------------+-------+------+
| blue | 2017-06-22 11:24:50 | 5 | 3 |
| green | 2017-06-25 10:47:50 | 4 | 3 |
| yellow | 2017-06-21 09:23:43 | 4 | 2 |
| red | 2017-06-20 13:52:18 | 3 | 1 |
+------------------------------+---------------------+-------+------+
检查日期是否为最近的浅色,深色,中等或无色调。
如果我在mysql之前或之后在PHP中执行此操作,它会使结果产生偏差,因此必须在mysql中。
也许我可以以某种方式在mysql中使用正则表达式?
谢谢:)
答案 0 :(得分:2)
您可以使用SUBSTRING_INDEX
执行此操作。
有一个例子
表
value color
10 light-red
4 red
7 red
2 light-pink
4 dark-pink
6 medium-pink
6 pink
SELECT SUBSTRING_INDEX( color, '-', -1) AS color, SUM( value ) as Total
FROM count
GROUP BY SUBSTRING_INDEX( color, '-', -1)
输出
color Total
pink 18
red 21
http://www.w3resource.com/mysql/string-functions/mysql-substring_index-function.php
答案 1 :(得分:1)
你可以使用
SUBSTRING_INDEX('light-green','-',-1)
实现此功能,使用select和group by子句中的函数。
答案 2 :(得分:1)
您的查询中的修改后应该有效:
SELECT SUBSTRING_INDEX(color,&#39; - &#39;, - 1)AS single_color,
max(check_date)AS check_date,
计数(*)AS总计,
sum(color_code&lt; 1 OR color_code&gt; = 300)AS cnt
FROM check_colors
GROUP BY SUBSTRING_INDEX(颜色,&#39; - &#39;, - 1)
订购cnt DESC LIMIT 10;