需要Mysql查询优化和更新

时间:2015-09-23 07:09:37

标签: php mysql codeigniter

这是我的Codeigniter查询

 $this->db->select('o.*,seo.price as seo_price,SUM(domain.price) as domain_price,SUM(bes.price) as bes_price, SUM(op.price) as payment_price');
    $this->db->from('order as o');
    $this->db->join("order_package_seo as seo", "seo.order_id = o.id", "left");
    $this->db->join("order_package_domain as domain", "domain.order_id = o.id", "left");
    $this->db->join("order_package_bespoke as bes", "bes.order_id = o.id", "left");
    $this->db->join("order_payment as op", "op.order_id = o.id", "left"); 
    $this->db->where("o.id" , $id);
    $query = $this->db->get();
    return $query->row();

我在" order_package_beskope"中有3条记录。表和其他所有的1 我想通过 Order_id 从所有表中获得价格 我需要使用group_by distinct或任何其他需要你建议的方法。

1 个答案:

答案 0 :(得分:0)

更改第一个语句

$this->db->select('o.*,seo.price as seo_price,domain.price ,SUM(bes.price) as bes_price, op.price as payment_price');
$this->db->from('order as o');
$this->db->join("order_package_seo as seo", "seo.order_id = o.id", "left");
$this->db->join("order_package_domain as domain", "domain.order_id = o.id", "left");
$this->db->join("order_package_bespoke as bes", "bes.order_id = o.id", "left");
$this->db->join("order_payment as op", "op.order_id = o.id", "left"); 
$this->db->where("o.id" , $id);
$this->db->group_by("o.id");
$query = $this->db->get();
return $query->row();

因为,你提到表“order_package_beskope”有3条记录,所以只有那个表字段“bes_price”需要SUM。所有其他表只有一个记录,因此如果添加SUM,它将乘以3(order_package_bespoke记录的数量)。

因此,从SELECT中的其他表字段中删除SUM,并按o.id

添加组