对于负值,请使用reverse()以及concat()。让我们首先创建一个表-
create table DemoTable632 ( histogramId int NOT NULL AUTO_INCREMENT PRIMARY KEY,histogramValue int,histogramImage text );
使用插入命令在表中插入一些记录-
insert into DemoTable632(histogramValue) values(2); insert into DemoTable632(histogramValue) values(3); insert into DemoTable632(histogramValue) values(-6); insert into DemoTable632(histogramValue) values(-5);
使用select语句显示表中的所有记录-
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)
这是对带有负值的直方图的查询-
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)