MySQL Convert Bytes to Kilobytes, Megabytes, Gigabytes

后端 未结 4 2035
旧时难觅i
旧时难觅i 2020-12-11 02:28

I have a logs table that stores various file sizes in bytes. I want to be able to query the database and return the smallest possible float which has been converted to MB GB

4条回答
  •  孤街浪徒
    2020-12-11 02:42

    I know this is an old question, but I was looking for the same thing recently, and found out that MySQL 5.7 added format_bytes function exactly for this purpose:

    mysql> SELECT format_bytes(512), format_bytes(18446644073709551615);
    +-------------------+------------------------------------+
    | format_bytes(512) | format_bytes(18446644073709551615) |
    +-------------------+------------------------------------+
    | 512 bytes         | 16383.91 PiB                       |
    +-------------------+------------------------------------+
    

提交回复
热议问题