简体   繁体   English

MySQL查询优化和更新需要

[英]Mysql Query optimization and update needed

Here is my Codeigniter Query 这是我的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();

I have 3 records in "order_package_beskope" table and 1 in others all I want to get price from all table by Order_id is i need to use group_by distinct or any other method need you suggestion. 我在“ order_package_beskope”表中有3条记录,在其他1条中,我想通过Order_id从所有表中获取价格是我需要使用group_by或其他任何您需要的方法。

Change the first statement like this 像这样更改第一个语句

$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();

Because, you mentioned the table "order_package_beskope" has 3 records, so only that table field "bes_price" needs the SUM. 因为您提到表“ order_package_beskope”具有3条记录,所以只有表字段“ bes_price”需要SUM。 All the other tables has only one record so if you add SUM, it will get multiplied by 3(number of order_package_bespoke records). 所有其他表只有一条记录,因此,如果添加SUM,它将乘以3(order_package_bespoke记录数)。

So remove the SUM from other tables fields in SELECT and add group by o.id 因此,请从SELECT中的其他表字段中删除SUM并按o.id添加group

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM