简体   繁体   中英

join three tables in codeigniter with one to many relation

I am doing a project in codeigniter.Here I want to join three tables

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

campaign(id, name, adminId) and

order(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,

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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