Mysql取2位小數,加百分號,結果加序號

1、取2位小數

方式一 :

select truncate(data/total,2) 

實際使用中發現丟失精度

方式二 :

select convert(data/total,decimal(10,2))

推薦此方法,數據準確

2、拼接%

select concat( truncate(1/3,2)*100 ,'%')

3、查詢結果加序號

select   (@i:=@i+1) i ,data ,total from mytable ,(SELECT @i:=0) as i 

4、整理SQL

select
    (@i:=@i+1) i,
    data,
    total,
    concat(convert((data/total)*100,decimal(10,2)),'%') as rate
    from
        ( select 
                data,
                sum(data) total,
                from  mytable
                group by mytype order by data desc
        ) temp,
    (SELECT @i:=0) as i

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章