php - codeigniter 数据库查询替换为现有的

标签 php mysql database codeigniter

$this->db->select('SUM( COALESCE( purchase_unit_cost, 0 ) * quantity ) AS cost, SUM( COALESCE( sale_unit_price, 0 ) * quantity ) AS sales, SUM( COALESCE( purchase_net_unit_cost, 0 ) * quantity ) AS net_cost, SUM( COALESCE( sale_net_unit_price, 0 )*quantity ) AS net_sales', FALSE);

if($date){
$this->db->where('costing.date', $date);
}
$q = $this->db->get('costing');
if ($q->num_rows() > 0) {
            return $q->row();
}

我想用这个替换上面的代码:

select SUM( COALESCE( sma_costing.purchase_unit_cost, 0 ) * sma_costing.quantity ) AS cost, SUM( COALESCE( sma_costing.sale_unit_price, 0 ) * sma_costing.quantity ) AS sales, SUM( COALESCE( sma_costing.purchase_net_unit_cost, 0 ) * sma_costing.quantity ) AS net_cost, SUM( COALESCE( sma_costing.sale_net_unit_price, 0 ) * sma_costing.quantity ) AS net_sales
from  sma_costing
right join sma_sale_items on (sma_sale_items.id=sma_costing.sale_item_id and sma_sale_items.quantity=sma_costing.quantity)
left join sma_sales on sma_sale_items.sale_id=sma_sales.id
where sma_costing.date='$date'

已在 codeigniter 中尝试但未显示任何结果

$this->db->select('SUM( COALESCE( purchase_unit_cost, 0 ) * quantity ) AS cost, SUM( COALESCE( sale_unit_price, 0 ) * quantity ) AS sales, SUM( COALESCE( purchase_net_unit_cost, 0 ) * quantity ) AS net_cost, SUM( COALESCE( sale_net_unit_price, 0 )*quantity ) AS net_sales', FALSE);if ($date) {$this->db->where('costing.date', $date);}elseif ($month) {$this->load->helper('date'); $last_day = days_in_month($month, $year); $this->db->where('costing.date >=', $year.'-'.$month.'-01 00:00:00');$this->db->where('costing.date <=', $year.'-'.$month.'-'.$last_day.' 23:59:59');} if ($warehouse_id) {
        $this->db->join('sales', 'sales.id=costing.sale_id')
        ->where('sales.warehouse_id', $warehouse_id);
    }$q = $this->db->get('costing'); if ($q->num_rows() > 0) {
        return $q->row();}return false;   

这是我正在调整的代码

最佳答案

尝试使用 $this->db->query()

$this->db->query("SELECT SUM( COALESCE( sma_costing.purchase_unit_cost, 0 ) * sma_costing.quantity ) AS cost, SUM( COALESCE( sma_costing.sale_unit_price, 0 ) * sma_costing.quantity ) AS sales, SUM( COALESCE( sma_costing.purchase_net_unit_cost, 0 ) * sma_costing.quantity ) AS net_cost, SUM( COALESCE( sma_costing.sale_net_unit_price, 0 ) * sma_costing.quantity ) AS net_sales
                FROM  sma_costing
                RIGHT JOIN sma_sale_items on (sma_sale_items.id=sma_costing.sale_item_id AND sma_sale_items.quantity=sma_costing.quantity)
                LEFT JOIN sma_sales on sma_sale_items.sale_id=sma_sales.id
                WHERE sma_costing.date='$date' ");

关于php - codeigniter 数据库查询替换为现有的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42056491/

相关文章:

php mysql 注册表单邀请码

php - Magento 2 : I want to add ifconfig in override block xml

mysql - 限制子句的语法错误

sql - 用于插入脚本的生产就绪模板

MySQL数据库权限问题

php - 使用 PHP 更新 PostgreSQL 中的行

PHP加密&VB.net解密

php - 代码点火器 : compare timestamp value from database against current date

MySQL 为什么在有索引时记录为慢查询/日志查询不使用索引?

mysql - 对此最好的查询是什么?