【问题标题】:Codeigniter retrieve top 5 most occurring rows in columnCodeigniter 检索列中出现次数最多的前 5 行
【发布时间】:2020-09-05 21:04:28
【问题描述】:

我目前正在使用 codeigniter 3 来构建一个网站。我已经能够使用数据库和活动记录构建一个完整的画廊和购物车系统。以及后端。我目前正在使用 charts.js 在网站上 5 个最畅销产品的后端构建一个饼图。同样,我将使用折线图制作按小时销售的商品的图表。我可以使用虚拟数据使图表显示没有问题,但是我遇到了尝试从数据库中检索正确数据的问题。看来查询将非常复杂。

我的 mysql 表名为order_items。在订单项中有一个列qty 和一个列product_type。两者都返回整数。我需要的是找出哪五件商品的销量最高。最终,我需要将数据添加到我的 charts.js 设置中,如下所示。任何人都对 codeigniter 活动记录有高级经验?

var pieData = [{
    value : 300,
    color : "#F7464A",
    highlight : "#FF5A5E",
    label : "Product 1"
}, {
    value : 125,
    color : "#46BFBD",
    highlight : "#5AD3D1",
    label : "Product 2"
}, {
    value : 100,
    color : "#FDB45C",
    highlight : "#FFC870",
    label : "Product 3"
}, {
    value : 40,
    color : "#949FB1",
    highlight : "#A8B3C5",
    label : "Product 4"
}, {
    value : 20,
    color : "#4D5360",
    highlight : "#616774",
    label : "Product 5"
}];

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    我认为你应该试试这个脚本,

    $data = $this->db->select('*')
                     ->from('order_items,SUM(qty) as total_qty')
                     ->order_by('total_qty','desc')
                     ->limit(5)
                     ->group_by('product_id')
                     ->get()->result_array();
    

    【讨论】:

    • 我最初尝试过这样的事情,但我得到的只是最后 5 条记录,数量最多。它实际上并不查找产品 ID 的所有数量的总和。我想同时我可能已经使用 php 找到了我的解决方案
    • @user1881482,请看看我已经更新了我的答案
    • 我试过了,虽然我得到了这个错误You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SUM(qty) as total_qty GROUP BY product_id ORDER BY total_qty DESC LIMIT 5 at line 2
    【解决方案2】:

    我使用 php 找到了自己的解决方案,以防其他人偶然发现。如果有更好的方法使用 mysql 来做到这一点,我会很高兴学到一些东西。这是我的 php 函数得到我需要的结果。

    $query = $this -> db -> get('order_items');
    $array = array();
    foreach ($query -> result() as $result) {
        $key = $result -> product_id;
        $value = $result -> qty;
        if (!isset($array[$key])) {
            $array[$key] = $value;
        } else if (array_key_exists($key, $array)) {
            $old = $array[$key];
            $array[$key] = $value + $old;
        } 
    }
    arsort($array);
    $newArray = array_slice($array, 0, 5, true);
    return $newArray;
    

    在过去的几个小时里,我做了一些 mysql 教程,学到了很多关于 join、order 和 limit 的知识。下面是我最终用来得到我需要的东西。

    $select = array('products.name as label', 'sum(order_items.qty) as value');
    $final = $this -> db -> select($select)
                   -> from('products') 
                   -> join('order_items', 'order_items.product_id = products.id', 'left') 
                   -> group_by('products.id') 
                   -> order_by('value', 'DESC') 
                   -> limit(5) 
                   -> get() -> result_array();
    

    【讨论】:

      【解决方案3】:

      试试这个

      $data = $this->db->select('id, product_id, SUM(quantity) as total_qty')
                       ->from('table')
                       ->order_by('total_qty','desc')
                       ->limit(5)
                       ->group_by('product_id')
                       ->get()->result_array();
      

      【讨论】:

        【解决方案4】:
        public function getAllBestsaller() {  
        
            $result = array();
            $returndata = array();
            $this->db->select("*, SUM(total_sale) as total_sale");
            $this->db->from("{$this->tblproducts}");
        
            $this->db->where('isactive', 1 );
            $this->db->order_by('total_sale','desc');
            $this->db->limit(2);
            $this->db->group_by('product_id');
            $aQuery = $this->db->get();
            $this->db->last_query();
            if($aQuery -> num_rows() >0 ){
                $returndata = $aQuery->result();
            }
            return $returndata;
        }
        

        【讨论】:

          猜你喜欢
          • 2016-12-14
          • 2018-11-06
          • 2019-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-06
          相关资源
          最近更新 更多