以下是myTable表中的内容。
+----------+--------+--------+--------+--------+--------+---------+
| date | value1 | value2 | value3 | value4 | value5 | value6 |
+----------+--------+--------+--------+--------+--------+---------+
|2015-05-01| 2 | 10 | 20 | 1 | 40 | 50 |
|2015-05-02| 5 | 12 | 22 | 32 | 42 | 52 |
|2015-05-03| 6 | 1 | 24 | 34 | 2 | 5 |
|2015-05-04| 8 | 16 | 26 | 36 | 46 | 56 |
|2015-05-05| 1 | 18 | 28 | 38 | 48 | 58 |
|2015-05-06| 3 | 11 | 1 | 31 | 41 | 51 |
|2015-05-07| 5 | 13 | 23 | 3 | 43 | 53 |
|2015-05-08| 9 | 15 | 25 | 35 | 45 | 55 |
|2015-05-09| 4 | 3 | 27 | 37 | 47 | 3 |
|2015-05-10| 2 | 19 | 29 | 1 | 49 | 59 |
+----------+--------+--------+--------+--------+--------+---------+
这个输出,我想得到。但是如何?
+--------------------------------+--------------------------------+
| Output all table | Output last 5 days |
+--------------------------------+--------------------------------+
| number 1 (5 times) | number 1 (3 times) |
| number 3 (4 times) | number 2 (2 times) |
| number 2 (3 times) | number 5 (2 times) |
| number 5 (3 times) | |
+--------------------------------+--------------------------------+
我可以在mysql中实现这个吗?
答案 0 :(得分:0)
步骤(可以帮助):
1)确定要为类似值考虑的所有列。
2)按列计数创建具有计数的组以创建数组(值=>计数,...)
3)创建所有阵列后。合并它们以创建最终数组。 (将计数加起来得到最终计数)
答案 1 :(得分:0)
表中的列只能通过列名末尾的数字区分,这通常表示数据结构存在问题。通常,您希望每个日期和值有一行,称为联结表。
您可以通过取消隐藏数据然后汇总结果来执行您想要的操作:
select value, count(*)
from ((select date, value1 as value from mytable) union all
(select date, value2 from mytable) union all
(select date, value3 from mytable) union all
(select date, value4 from mytable) union all
(select date, value5 from mytable) union all
(select date, value6 from mytable)
) t
group by value
order by count(*) desc;
如果您想要特定时间段的直方图,可以在外部查询或所有子查询上添加where
子句。