gpt4 book ai didi

Mysql根据前缀计算总内存

转载 作者:行者123 更新时间:2023-11-29 23:45:46 24 4
gpt4 key购买 nike

有这个表:

 CREATE TABLE IF NOT EXISTS `assets` (
`asset_id` bigint(20) NOT NULL AUTO_INCREMENT,
`asset_memory` varchar(255) NOT NULL,
`asset_memory_prefix` tinyint(1) NOT NULL DEFAULT '2',
PRIMARY KEY (`asset_id`),
KEY `Locations` (`position_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

我需要计算特定 asset_id 的内存总和asset_memory 和 asset_memory_prefix 其中 asset_memory_prefix 定义如下:

    $_prefix = array(
array("id" => 0, "prefix" => "B", "prefix_name" => "byte", "pow_value" => "0"),
array("id" => 1, "prefix" => "Kb", "prefix_name" => "Kilobyte", "pow_value" => "1"),
array("id" => 2, "prefix" => "Mb", "prefix_name" => "Megabyte", "pow_value" => "2"),
array("id" => 3, "prefix" => "Gb", "prefix_name" => "Gigabyte", "pow_value" => "3"),
array("id" => 4, "prefix" => "Tb", "prefix_name" => "Terabyte", "pow_value" => "4"),
array("id" => 5, "prefix" => "Pb", "prefix_name" => "Petabyte", "pow_value" => "5"),
array("id" => 6, "prefix" => "Eb", "prefix_name" => "Exabyte", "pow_value" => "6"),
array("id" => 7, "prefix" => "Zb", "prefix_name" => "Zettabyte", "pow_value" => "7")
);

所以条目可能是这样的:

    asset_id, asset_memory, asset_memory_prefix
1, 1024, 2
2, 1024, 3
3, 2048, 2

如何在 SELECT 中执行此操作?

最佳答案

您只想要分组依据吗?

select asset_id, sum(asset_memory) as asset_memory, asset_memory_prefix
from assets
group by asset_id, asset_memory_prefix;

编辑:

我认为这就是您想要的,假设 asset_memory_prefix 是一个表:

select a.asset_id,
sum(pow(1000, ap.pow_value) * asset_memory) as total_memory_bytes
from assets a join
asset_prefix ap
on a.asset_memory_prefix = ap.asset_memory_prefix
group by a.asset_id;

我不确定您想要 1,000 还是 1,024 (2^10)。

您应该能够直接使用asset_memory_prefix:

select a.asset_id,
sum(pow(1024, ap.asset_memory_prefix) *
asset_memory
) as total_memory_bytes
from assets a join
asset_prefix ap
on a.asset_memory_prefix = ap.asset_memory_prefix
group by a.asset_id;

关于Mysql根据前缀计算总内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com