简体   繁体   English

在codeigniter中使用一对多关系连接三个表

[英]join three tables in codeigniter with one to many relation

I am doing a project in codeigniter.Here I want to join three tables 我在codeigniter做一个项目。我想加入三个表

clients(id, name, email, adminId, campaignId, dateAdded, is_deleted) 客户(id,name,email,adminId,campaignId,dateAdded,is_deleted)

campaign(id, name, adminId) and 广告系列(ID,名称,adminId)和

order(id, name, cost, dateAdded, clientId). 订单(id,name,cost,dateAdded,clientId)。

From these tables i want to select(in between two dates) the number of clients added,campaign name and total order cost of a client.When I joined the two tables (clients and campaign) it returns the correct result. 从这些表中我想选择(在两个日期之间)添加的客户数量,活动名称和客户的总订单成本。当我加入两个表(客户和活动)时,它返回正确的结果。

The query I used is 我使用的查询是

$this->db->select('clients.id AS my_client,
    clients.name AS client_name,
    campaign.name AS campaign_name,
    DATE(clients.dateAdded) as client_date,
    COUNT(clients.id) AS num_rows');

$this->db->from('clients');
$this->db->where('clients.adminId', $adminId);
$this->db->where('DATE(clients.dateAdded) >=', $from_date);
$this->db->where('DATE(clients.dateAdded) <=', $to_date);
$this->db->join('campaign', 'campaign.id = clients.campaignId', 'left');
$this->db->group_by('campaign_name');
$query = $this->db->get();
return $query->result();

But when I joined three tables(clients, campaign, order) it is not returning correct result.The relation between client and order is one to many.Ie one client can have more than one order.So it will not give correct value for total number of clients added between two dates.The join query I used to join three tables is 但当我加入三个表(客户,活动,订单)时,它没有返回正确的结果。客户和订单之间的关系是一对多的。如果一个客户可以有多个订单。所以它不会给出正确的总值在两个日期之间添加的客户端数。我用来连接三个表的连接查询是

$this->db->select('clients.id AS my_client,
    clients.name AS client_name,
    campaign.name AS campaign_name,
    DATE(clients.dateAdded) AS client_date,
    SUM(order.cost) AS order_cost,
    COUNT(clients.id) AS num_rows');

$this->db->from('clients');
$this->db->where('clients.adminId', $adminId);
$this->db->where('clients.is_deleted', 0);
$this->db->where('DATE(clients.dateAdded) >=', $from_date);
$this->db->where('DATE(clients.dateAdded) <=', $to_date);
$this->db->join('campaign', 'campaign.id = clients.campaignId', 'left');
$this->db->join('order', 'order.clientId = clients.id', 'left');
$this->db->group_by('campaign_name');
$query = $this->db->get();
return $query->result();

Can anyone have some idea to do this.Thanks in advance 任何人都可以有一些想法这样做。谢谢你提前

I got the expected result by counting distinct client.id (COUNT(DISTINCT(clients.id))) for the number of clients added, 我通过计算不同的client.id(COUNT(DISTINCT(clients.id)))来获得预期的结果,用于添加的客户端数量,

$this->db->select('clients.id AS my_client,
    clients.name AS client_name,
    campaign.name AS campaign_name,
    DATE(clients.dateAdded) AS client_date,
    SUM(order.cost) AS order_cost,
    COUNT(DISTINCT(clients.id)) AS num_rows'); //changed the code here from COUNT(clients.id) AS num_rows

$this->db->from('clients');
$this->db->where('clients.adminId', $adminId);
$this->db->where('clients.is_deleted', 0);
$this->db->where('DATE(clients.dateAdded) >=', $from_date);
$this->db->where('DATE(clients.dateAdded) <=', $to_date);
$this->db->join('campaign', 'campaign.id = clients.campaignId', 'left');
$this->db->join('order', 'order.clientId = clients.id', 'left');
$this->db->group_by('campaign_name');
$query = $this->db->get();
return $query->result();

I dont think this is the correct way.Is there any better way to do this.Thanks for your support. 我不认为这是正确的方法。有没有更好的方法来做到这一点。谢谢你的支持。

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

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