【问题标题】:Codeigniter Count QueryCodeigniter 计数查询
【发布时间】:2019-02-14 06:12:51
【问题描述】:

如何在 Codeigniter 中构建以下查询。我似乎无法弄清楚。 这是我模型中的功能:

public function update_daily_inventory(){
foreach ($this->parseInventoryHtml() as $item)
    {   
        $_item = $this->db->query('SELECT COUNT(*) as `count` FROM product WHERE product_code = "'.$item['sku'].'"');
    }
        return $_item;
}

我想使用数组键作为 where 变量。如果这很简单,请道歉我是编码新手。这一直返回 0。

【问题讨论】:

    标签: php codeigniter activerecord


    【解决方案1】:
    $this->db->where('product_code', $item['sku']);
    $this->db->select('COUNT(*) as `count`');
    $_item =  $this->db->get('product')->row()->count;
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    【解决方案2】:

    count 不需要设置别名。这里是一个简单的查询来计算结果中找到的行数。

    $this->db->where('product_code', $item['sku']);
    $this->db->select('*');
    $_item =  $this->db->get('product')->num_rows();
    

    【讨论】:

      【解决方案3】:

      您可以使用以下代码来构建您的查询。

      $this->db->select('*');
      $this->db->where('product_code', $item['sku']);
      $_item = $this->db->count_all_results('product');  
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-09
        • 1970-01-01
        • 1970-01-01
        • 2012-07-25
        相关资源
        最近更新 更多