zoukankan      html  css  js  c++  java
  • [MySQL]如何将大数值带上 元,万,亿 这样的单位?

    要解决的问题: 某表某字段用来表示交易金额,不同记录的金额相差很大,有的只有几元几角几分,有的却上亿.如果直接就把数值在页面上展示出来,则可读性不佳.因此我们需要将其单位展示出来,如1.23元,3.45万,4.56亿等.

    解决方法:用case when判断数值所在区间,用convert函数将计算后的数值留下两位小数,用concat函数将计算后的数值和单位连接在一起.

    形成的SQL语句是:

    select id,value,(case when value<10000  then concat(convert(value,decimal(12,2)),'')
                 when value<100000000 then concat(convert(value/10000,decimal(12,2)),'')
                 else concat(convert(value/100000000,decimal(12,2)),'亿') end) as newCol from tb_number ;

    执行效果:

    mysql> select id,value,(case when value<10000  then concat(convert(value,decimal(12,2)),'')
        ->              when value<100000000 then concat(convert(value/10000,decimal(12,2)),'')
        ->  else concat(convert(value/100000000,decimal(12,2)),'亿') end) as newCol from tb_number ;
    +----+------------------+--------------+
    | id | value            | newCol       |
    +----+------------------+--------------+
    |  1 |             1.08 | 1.08元        |
    |  2 |            12.08 | 12.08元       |
    |  3 |           123.08 | 123.08元      |
    |  4 |          1234.08 | 1234.08元     |
    |  5 |         12345.08 | 1.23万         |
    |  6 |        123456.08 | 12.35万        |
    |  7 |       1234567.08 | 123.46万       |
    |  8 |      12345678.08 | 1234.57万      |
    |  9 |     123456789.08 | 1.23亿         |
    | 10 |    1234567891.08 | 12.35亿        |
    | 11 |   12345678912.08 | 123.46亿       |
    | 12 |  123456789123.08 | 1234.57亿      |
    | 13 | 1234567891234.08 | 12345.68亿     |
    | 14 | 12345678912345.1 | 123456.79亿    |
    | 16 |  123456789123456 | 1234567.89亿   |
    +----+------------------+--------------+
    15 rows in set (0.00 sec)

    以上sql用到的表和数据:

    create table tb_number(
        id int primary key,
        value double )
        
    insert into tb_number(id,value) values('1', '1.08');
    insert into tb_number(id,value) values('2', '12.08');
    insert into tb_number(id,value) values('3', '123.08');
    insert into tb_number(id,value) values('4', '1234.08');
    insert into tb_number(id,value) values('5', '12345.08');
    insert into tb_number(id,value) values('6', '123456.08');
    insert into tb_number(id,value) values('7', '1234567.08');
    insert into tb_number(id,value) values('8', '12345678.08');
    insert into tb_number(id,value) values('9', '123456789.08');
    insert into tb_number(id,value) values('10','1234567891.08');
    insert into tb_number(id,value) values('11','12345678912.08');
    insert into tb_number(id,value) values('12','123456789123.08');
    insert into tb_number(id,value) values('13','1234567891234.08');
    insert into tb_number(id,value) values('14','12345678912345.08');
    insert into tb_number(id,value) values('16','123456789123456.08');

    --2020年5月8日--

  • 相关阅读:
    org.hibernate.HibernateException: No Session found for current thread
    TCP/IP协议 HTTP协议
    [ERROR][org.springframework.web.context.ContextLoader][main] Context initialization failed org.sprin
    oracle 导出表结构和数据,使用plsql
    jar包里查找指定的class文件,排查是否存在或重复,工具软件:Java Class Finder
    maven手动安装jar到本地仓库
    The reference to entity “idNo” must end with the ';' delimiter 异常处理
    activeMQ下载,安装,启动,关闭
    Oracle错误:ORA-01033
    -Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m
  • 原文地址:https://www.cnblogs.com/heyang78/p/12850389.html
Copyright © 2011-2022 走看看