显示带有负值的 MySQL 直方图?
mysqlmysqli database
对于负值,使用 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)