【问题标题】:How do i sum column group by different column in Codeigniter?如何按 Codeigniter 中的不同列对列组求和?
【发布时间】:2020-12-30 02:26:32
【问题描述】:

我有一个像这样的 cart 表。所以,我正在尝试根据 'id_user' 列对 'weight' 列求和strong> 列。

id id_user id_product qty weight subtotal
1 1 1 6 3000 399960
2 1 4 1 500 41864
3 4 7 1 500 136350
4 4 2 3 1500 212100
5 4 4 2 1000 83728

我想要这样的结果:

id_user weight
1 3500
4 3000

我试过了,但结果不是我想要的。这是我的代码:

我的模特

public function getSum()
{
    $totalWeight = "SELECT id_user, sum(weight) as weight FROM cart GROUP BY id_user";
    $result = $this->db->query($totalWeight);
    return $result->row()->weight;
}


我的控制器
public function index() {
   $this->checkout->table  = 'cart';
   $data['totalWeight'] = $this->checkout->getSum();
}


我从上面的代码中得到的结果是这样的:
id_user weight
1 3500
4 3500

我不知道为什么,但是每次我尝试使用不同的quantity(qty)和不同的id_user结帐时,结果都会显示在重量栏中始终为 3500 或始终遵循执行第一个结帐过程的 id_user。



我希望你们能理解并帮助我解决我的问题。谢谢:)

【问题讨论】:

    标签: mysql sum codeigniter-3 cart checkout


    【解决方案1】:

    您的 sql 查询计算表中所有用户的权重总和,但您的 php 代码只检索结果集中的第一条记录 - 这很可能是第一个订购的用户。

    如果您只想检索特定用户的总和,请按id_user 过滤您的查询。

    public function getSum()
    {
        $totalWeight = "SELECT id_user, sum(weight) as weight FROM cart WHERE id_user= ? GROUP BY id_user";
        $result = $this->db->query($totalWeight, array($current_user_id));
        return $result->row()->weight;
    }
    

    如果要检索所有用户的总和,则需要使用查询结果的result() 方法并循环获取权重。

    public function getSum()
    {
        $totalWeight = "SELECT id_user, sum(weight) as weight FROM cart GROUP BY id_user WHERE id_user= ?";
        $result = $this->db->query($totalWeight, array($current_user_id));
        return $result->result();
    }
    

    【讨论】:

    • 感谢您的回答,我认为它有效。但是出现一个错误:发生数据库错误 错误编号:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的“WHERE id_user = 4”附近使用正确的语法 SELECT id_user, sum(weight) as weight FROM cart GROUP BY id_user WHERE id_user = 4 文件名:C:/xampp /htdocs/omid/system/database/DB_driver.php 行号:691
    • 道歉,在group by之前。更正了语法。
    • If you want to retrieve the sums for all users, then you need to use the result() method of your query results and get the weights in a loop. 肯定不是。这将是极其低效的。在该实例中包含 GROUP BY 是没有意义的
    • @Strawberry 查询已经进行了求和,result() 方法只检索结果。您确实需要循环来处理总和。
    • 我认为您尚未查看自己的查询
    【解决方案2】:

    伙计,如果我有两张表,一张用于供应商,另一张用于他们的采购发票一对多关系。 每个供应商都有很多发票,我想得到他们的会费我使用以下查询并得到正确的结果希望这会对你有所帮助。

    $this->db->select('supliers.SID,supliers.companyName, supliers.address, supliers.mobile, (sum(coalesce(suplierinvoice.newTotal,0)) - sum(coalesce(suplierinvoice.paidAmount, 0))) as dues ');
        $this->db->from('supliers');
        $this->db->join('suplierinvoice','suplierinvoice.suplierID = supliers.SID', 'left');
        $this->db->group_by('suplierinvoice.suplierID');
        $query=$this->db->get();
        return $query->result_array();
    

    group_by 将发挥其作用,将所有与同一供应商ID 相关的记录组合起来。如果您想获得一条记录,只需添加 where 条件即可。

    【讨论】:

      猜你喜欢
      • 2020-02-11
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多