gpt4 book ai didi

php - 我们如何将这两个表结果合并为一个

转载 作者:行者123 更新时间:2023-11-29 15:39:07 26 4
gpt4 key购买 nike

这里我想将这两个查询结果连接成一个结果第一个查询如下所示

   $this->db->select('bills.date as d_date,bill_details.agent_name,
SUM(bill_details.profit) AS total_profit');
$this->db->join('bill_details', 'bill_details.bill_id=bills.id','left');
$this->db->where('DATE(bills.date) >=', $start_date);
$this->db->where('DATE(bills.date) <=', $end_date);
$this->db->group_by('Date(bills.date)');
$this->db->group_by('bills.customerid');
$query1 = $this->db->get('bills')->result();
return $query1;

结果看起来像这样

Name    Date          Purchase  
Ned 2019-07-26 210.60

第二个查询如下所示

    $this->db->select('assigned_result.date,assigned_result.user_id,SUM(assigned_result.total_price) AS t_price,SUM(assigned_result.total_dc) AS t_dc');
$this->db->where('DATE(assigned_result.date) >=', $start_date);
$this->db->where('DATE(assigned_result.date) <=', $end_date);

$this->db->group_by('assigned_result.user_id');
$query2 = $this->db->get('assigned_result')->result();

第二个查询结果如下所示

Name    Date          winning
Ned 2019-07-26 120

现在我想组合这样的查询

Name    Date          Purchase   winning 
Ned 2019-07-26 210.60 120

为了获得这个结果,我加入了查询并像这样返回

$this->db->select('bills.date as d_date,bill_details.agent_name,
SUM(bill_details.profit) AS total_profit,SUM(assigned_result.total_price) AS t_price,SUM(assigned_result.total_dc) AS t_dc');
$this->db->join('bill_details', 'bill_details.bill_id=bills.id','left');
$this->db->join('assigned_result', 'bills.id=assigned_result.bill_no', 'left');
$this->db->where('DATE(bills.date) >=', $start_date);
$this->db->where('DATE(bills.date) <=', $end_date);
$this->db->group_by('Date(bills.date)');
$this->db->group_by('bills.customerid');
$query1 = $this->db->get('bills')->result();
return $query1;

但我得到这样的结果

Name    Date          Purchase  winning
Ned 2019-07-26 226 160

购买金额获取错误。

最佳答案

根据您的 SQL 编辑器 数据,下一个查询适合您:

select 
B.user_id,
B.agent_name,
sum(B.profit),
C2.tprice,
C2.tdc
from B
join A on A.id = B.bill_id and A.customerid = B.user_id
left join ( select
C.user_id,
C.data_id,
C.bill_no,
sum(C.total_price) tprice,
sum(C.total_dc) tdc from C) C2 on C2.data_id = B.id and C2.user_id = B.user_id
group by B.user_id;

PHP: ( reference )

    $this->db->select('DATE(bills.date) as d_date,
bill_details.user_id,
bill_details.agent_name,
SUM(bill_details.profit) AS total_profit,
C2.tprice AS t_price,
C2.tdc AS t_dc');

$this->db->join('bills', 'bills.id = bill_details.bill_id and bills.customerid = bill_details.user_id','left');
$this->db->join('(select
assigned_result.user_id,
assigned_result.data_id,
assigned_result.bill_no,
sum(assigned_result.total_price) tprice,
sum(assigned_result.total_dc) tdc from assigned_result) as C2','C2.data_id = bill_details.id and C2.user_id = bill_details.user_id', 'LEFT');

$this->db->where('DATE(bills.date) >=', $start_date);
$this->db->where('DATE(bills.date) <=', $end_date);

$this->db->group_by('bill_details.user_id)');

$query1 = $this->db->get('bill_details')->result();
return $query1;

    $this->db->select('assigned_result.user_id, 
assigned_result.data_id,
assigned_result.bill_no,
sum(assigned_result.total_price) tprice,
sum(assigned_result.total_dc) tdc')
->from('assigned_result');

$subquery = $this->db->_compile_select();
$this->db->_reset_select();

$this->db->select('DATE(bills.date) as d_date,
bill_details.user_id,
bill_details.agent_name,
SUM(bill_details.profit) AS total_profit,
C2.tprice AS t_price,
C2.tdc AS t_dc');

$this->db->join('bills', 'bills.id = bill_details.bill_id and bills.customerid = bill_details.user_id','left');
$this->db->join("($subquery) C2",'C2.data_id = bill_details.id and C2.user_id = bill_details.user_id', 'LEFT');

$this->db->where('DATE(bills.date) >=', $start_date);
$this->db->where('DATE(bills.date) <=', $end_date);

$this->db->group_by('bill_details.user_id)');

$query1 = $this->db->get('bill_details')->result();
return $query1;

关于php - 我们如何将这两个表结果合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57872200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com