Reputation: 607
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
Upvotes: 3
Views: 3275
Reputation: 607
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.
Upvotes: 2