如何求和并创建Pecentage Codeigner



如何求和并创建2列之间的百分比?我使用codeigniter并在视图文件夹中创建这是我的代码

<?php
$target= "SELECT SUM(a) FROM table_a WHERE a = '1'";
$total= "SELECT SUM(b) FROM table_a WHERE b= '1'";
$query = $this->db->select('concat(round('.$total.'/'.$target.')*100) as data_percentage');
echo $this->db->get()->row()->data_percentage;?>
?>

请帮助

您可以使用这样的子查询:

$query = "(SELECT IFNULL(SUM(`a`), 0) FROM table_a WHERE a='1') AS sum_a,
(SELECT IFNULL(SUM(`b`), 0) FROM table_a WHERE b='1') AS sum_b,
concat(round( (SELECT sum_b) / (SELECT sum_a) )*100) as data_percentage";
$query = $this->db->select($query);
$result = $query->get('table_a');
echo $result->row()->data_percentage;

相关内容

  • 没有找到相关文章

最新更新