用负值显示 MySQL 直方图?
对于负值,请将 reverse() 与 concat() 结合使用。我们首先创建一个表 −
mysql> create table DemoTable632 ( histogramId int NOT NULL AUTO_INCREMENT PRIMARY KEY,histogramValue int,histogramImage text ); Query OK, 0 rows affected (0.78 sec)
使用 insert 命令将一些记录插入表中 −
mysql> insert into DemoTable632(histogramValue) values(2); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable632(histogramValue) values(3); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable632(histogramValue) values(-6); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable632(histogramValue) values(-5); Query OK, 1 row affected (0.13 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable632;
这将产生以下输出 −
+-------------+----------------+----------------+ | histogramId | histogramValue | histogramImage | +-------------+----------------+----------------+ | 1 | 2 | NULL | | 2 | 3 | NULL | | 3 | -6 | NULL | | 4 | -5 | NULL | +-------------+----------------+----------------+ 4 rows in set (0.00 sec)
以下是带有负值的直方图查询 −
mysql> SELECT histogramId, histogramValue, CONCAT( REVERSE(RPAD(REPEAT('*', IF(histogramValue<0,-histogramValue,0)), 100, ' ')), REPEAT('*',IF(histogramValue<0,0,histogramValue)) ) AS histogramImage FROM DemoTable632 ORDER BY histogramId;
这将产生以下输出 −
+-------------+----------------+----------------+ | histogramId | histogramValue | histogramImage | +-------------+----------------+----------------+ | 1 | 2 | ** | | 2 | 3 | *** | | 3 | -6 | ****** | | 4 | -5 | ***** | +-------------+----------------+----------------+ 4 rows in set (0.00 sec)
广告