【问题标题】:How to merge multiple function in single query?如何在单个查询中合并多个函数?
【发布时间】:2017-11-24 12:40:49
【问题描述】:

我试图在一个函数中组合 3-4 个函数并在另一个函数中调用它。例如

public function todayordercount(){       
  $sql  =  "SELECT *,count(id) FROM `tbl_order` WHERE DATE(`date_added`) = CURDATE() and status='Delivered'";
  $query   = $this->db->query($sql);
  if($query->num_rows()>0){
    return  $query->result_array();
  }else{
    return $query->row_array(); 
  }       
}

public function currentmonthcount(){       
  $sql  =  "SELECT sum(amount) as amt,sum(shipping) as shiping,sum(commission) as commission,sum(base_price) as base_price,sum(tax_amt) as tax,SUM(LENGTH(product_id) - LENGTH(REPLACE(product_id, ',', '')) + 1) as unit , count(id) FROM `tbl_order` WHERE MONTH(CURDATE())=MONTH(date_added) and status='Delivered'";
  $query   = $this->db->query($sql);

  if($query->num_rows()>0){
    return  $query->result_array();
  }else{
    return $query->row_array(); 
  }       
}

【问题讨论】:

  • 详细阐述您的问题以获得更好的清除...
  • SELECT *, COUNT(id) 没有 GROUP BY 是奇怪的 SQL.. 当 MySQL 服务器正在运行时,它不是未来的证明 sql_mode = 'ONLY_FULL_GROUP_BY' ... 此外,SUM(LENGTH(product_id) - LENGTH(REPLACE(product_id, ',', '')) + 1) as unit 使用 CSV 看起来像糟糕的数据库设计一个数据库。

标签: php mysql arrays codeigniter


【解决方案1】:
  public function mergeordercount(){

 $query1 =  $this->db->query("SELECT *,count(id) as today_order FROM `tbl_order` WHERE DATE(`date_added`) = CURDATE() and status='Delivered'");

 $query2 =  $this->db->query("SELECT sum(amount) as amt,sum(shipping) as shiping,sum(commission) as commission,sum(base_price) as base_price,sum(tax_amt) as tax,SUM(LENGTH(product_id) - LENGTH(REPLACE(product_id, ',', '')) + 1) as unit , count(id) FROM `tbl_order` WHERE MONTH(CURDATE())=MONTH(date_added) and status='Delivered'");

$query3 =  $this->db->query("SELECT count(status) as status FROM `tbl_order` WHERE MONTH(CURDATE())=MONTH(date_added) and status='Return'");    

$query4 =  $this->db->query("SELECT count(response) as calls FROM `tbl_order` WHERE response='pending' or response='hold'");        

$query5 =  $this->db->query("SELECT count(id) as product FROM `tbl_product`");    

 $result1   =  $query1->row_array();    
 $result2   =  $query2->row_array();    
 $result3   =  $query3->row_array();    
 $result4   =  $query4->row_array();         
 $result5   =  $query5->row_array();     
 return array_merge($result1, $result2, $result3, $result4, $result5); 
 }

你应该看起来像上面的代码,它会帮助你下注。 谢谢...

【讨论】:

    猜你喜欢
    • 2013-09-06
    • 2015-03-18
    • 2022-08-16
    • 2023-02-14
    • 2017-03-09
    • 2021-05-19
    • 2017-10-09
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多