我有一个日志表,以字节为单位存储各种文件大小。我希望能够查询数据库并返回已经转换为MB GB等的最小浮点数。目前我可以返回以MB为单位的值但是如何继续进一步划分为最小值并附加单位? / p>
SELECT CONCAT( ROUND( SUM( data_transferred ) /1048576, 2 ) , ' MB' )
FROM `logs`
任何帮助都将不胜感激。
更新:
基于链接voodoo417,我将查询更新为以下内容,将最相关的文件大小输出到两个小数位并附加单位(1000字节,1 KB,500 MB,2 GB等):< / p>
SET @bytes := (SELECT SUM(data_transferred) FROM wp_ddownload_statistics);
SELECT
CASE
WHEN ABS(@bytes) < 1024 THEN CONCAT( ROUND( @bytes, 2 ), ' Bytes')
WHEN ABS(@bytes) < 1048576 THEN CONCAT( ROUND( (@bytes/1024), 2 ), ' KB')
WHEN ABS(@bytes) < 1073741824 THEN CONCAT( ROUND( (@bytes/1048576), 2 ), ' MB')
WHEN ABS(@bytes) < 1099511627776 THEN CONCAT( ROUND( (@bytes/1073741824), 2 ), ' GB' )
WHEN ABS(@bytes) < 1125899906842624 THEN CONCAT( ROUND( (@bytes/1099511627776), 2 ), ' TB')
WHEN ABS(@bytes) < 1152921504606846976 THEN CONCAT( ROUND( (@bytes/1125899906842624), 2 ), ' PB' )
WHEN ABS(@bytes) < 1180591620717411303424 THEN CONCAT( ROUND( (@bytes/1152921504606846976) ,2), ' EB' )
WHEN ABS(@bytes) < 1208925819614629174706176 THEN CONCAT( ROUND( (@bytes/1180591620717411303424), 2), ' ZB' )
WHEN ABS(@bytes) < 1237940039285380274899124224 THEN CONCAT( ROUND( (@bytes/1208925819614629174706176), 2), ' YB' )
WHEN ABS(@bytes) < 1267650600228229401496703205376 THEN CONCAT( ROUND( (@bytes/1237940039285380274899124224), 2), ' BB' )
END
答案 0 :(得分:5)
我知道这是一个老问题,但我最近也在寻找相同的东西,并且发现MySQL 5.7为此目的添加了format_bytes
功能:
mysql> SELECT format_bytes(512), format_bytes(18446644073709551615);
+-------------------+------------------------------------+
| format_bytes(512) | format_bytes(18446644073709551615) |
+-------------------+------------------------------------+
| 512 bytes | 16383.91 PiB |
+-------------------+------------------------------------+
答案 1 :(得分:4)
我有一个更优雅的解决方案(也使用用户定义的功能):
CREATE FUNCTION `format_filesize`(filesize FLOAT) RETURNS varchar(20) CHARSET utf8
BEGIN
DECLARE n INT DEFAULT 1;
LOOP
IF filesize < 1024 THEN
RETURN concat(round(filesize, 2), ' ', elt(n, 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB'));
END IF;
SET filesize = filesize / 1024;
SET n = n + 1;
END LOOP;
END
更好,可以在外部程序中使用:
SET @filesize = 536870912;
SET @log = IFNULL(TRUNCATE(LOG(1024, @filesize), 0),0);
SELECT CONCAT(ROUND(@filesize / POW(1024, @log), 2), ' ',
ELT(@log + 1, 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB'));
答案 2 :(得分:3)
当filesize为0时,Renaat的代码失败(显然你不能从零开始做LOG)。因此@log填充为null,CONCAT也生成null。 正确的解决方法是:
SET @filesize = 536870912;
SET @log = IFNULL(TRUNCATE(LOG(1024, @filesize), 0),0);
SELECT CONCAT(ROUND(@filesize / POW(1024, @log), 2), ' ',
ELT(@log + 1, 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB'));
答案 3 :(得分:0)
尽管在 2016 年得到了回答,但遗憾的是,MariaDB 中仍未找到有关使用 format_bytes
的最佳答案。
我修改了 here 中的查询以创建一个我可以使用的函数。
FUNCTION `format_bytes`(val float) RETURNS varchar(20) CHARSET latin1
BEGIN
DECLARE pw smallint;
IF val < 1024 THEN
return CONCAT(val, ' B');
END IF;
SET pw = LEAST(7, FLOOR(LOG(val) / LOG(1024)));
RETURN CONCAT(ROUND(val / POW(1024, pw), 2), ' ', SUBSTR('KMGTPEZY', pw, 1), 'B');
END
>>> SELECT format_bytes(512), format_bytes(18446644073709551615);
+-------------------+------------------------------------+
| format_bytes(512) | format_bytes(18446644073709551615) |
+-------------------+------------------------------------+
| 512 B | 16.00 EB |
+-------------------+------------------------------------+
可以调整一些东西以更接近地模仿 MySQL format_bytes
,但这不是我的目标。
答案 4 :(得分:-1)
SELECT t.v.value('.','nvarchar(max)') as [Name Of Brand],
--t.v.value('@title','nvarchar(max)') as [Title Of Brand],
t.v.value('@href','nvarchar(max)') as [URL Local],
t.v.value('@tppabs','nvarchar(max)') as [URL Site]
FROM @htmlXML.nodes('div/div[5]/div[2]/a') as t(v)
union ALL
SELECT t.v.value('.','nvarchar(max)') as [Name Of Brand],
--t.v.value('@title','nvarchar(max)') as [Title Of Brand],
t.v.value('@href','nvarchar(max)') as [URL Local],
t.v.value('@tppabs','nvarchar(max)') as [URL Site]
FROM @htmlXML.nodes('div/div[6]/div[2]/a') as t(v)
union ALL SELECT t.v.value('.','nvarchar(max)') as [Name Of Brand],
--t.v.value('@title','nvarchar(max)') as [Title Of Brand],
t.v.value('@href','nvarchar(max)') as [URL Local],
t.v.value('@tppabs','nvarchar(max)') as [URL Site]
FROM @htmlXML.nodes('div/div[7]/div[2]/a') as t(v)
union ALL SELECT t.v.value('.','nvarchar(max)') as [Name Of Brand],
--t.v.value('@title','nvarchar(max)') as [Title Of Brand],
t.v.value('@href','nvarchar(max)') as [URL Local],
t.v.value('@tppabs','nvarchar(max)') as [URL Site]
FROM @htmlXML.nodes('div/div[8]/div[2]/a') as t(v)
union ALL SELECT t.v.value('.','nvarchar(max)') as [Name Of Brand],
--t.v.value('@title','nvarchar(max)') as [Title Of Brand],
t.v.value('@href','nvarchar(max)') as [URL Local],
t.v.value('@tppabs','nvarchar(max)') as [URL Site]
FROM @htmlXML.nodes('div/div[11]/div[2]/a') as t(v)
union ALL SELECT t.v.value('.','nvarchar(max)') as [Name Of Brand],
--t.v.value('@title','nvarchar(max)') as [Title Of Brand],
t.v.value('@href','nvarchar(max)') as [URL Local],
t.v.value('@tppabs','nvarchar(max)') as [URL Site]
FROM @htmlXML.nodes('div/div[12]/div[2]/a') as t(v)
union ALL SELECT t.v.value('.','nvarchar(max)') as [Name Of Brand],
--t.v.value('@title','nvarchar(max)') as [Title Of Brand],
t.v.value('@href','nvarchar(max)') as [URL Local],
t.v.value('@tppabs','nvarchar(max)') as [URL Site]
FROM @htmlXML.nodes('div/div[14]/div[2]/a') as t(v)
union ALL SELECT t.v.value('.','nvarchar(max)') as [Name Of Brand],
--t.v.value('@title','nvarchar(max)') as [Title Of Brand],
t.v.value('@href','nvarchar(max)') as [URL Local],
t.v.value('@tppabs','nvarchar(max)') as [URL Site]
FROM @htmlXML.nodes('div/div[15]/div[2]/a') as t(v)
例如:1024 = 1gb
select concat(round(data*1048576/1073741824,2),' GB')
1GB