Codeigniter: How to include SUM() and COUNT() in JOIN query? - php

I have this method in my Model
function get_users_details(){
$this->db->select("a.*,sum('b.downloads') as downloads,COUNT('b.user_email') as uploads");
$this->db->from('user a');
$this->db->join('files b', 'a.email = b.user_email','inner');
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach ($query->result_array() as $row){
$data[] = $row;
}
$query->free_result();
return $data;
}
else{
return false;
}
}
It only returns values from a single row while actually it supposed to return values from multiple rows.

sum() and count() are aggregate functions and will only return 1 row unless you combine it with a group_by statement.
SELECT count(*) FROM table_a
will return the total number of rows in table_a.
SELECT table_a.email, count(*) FROM table_a GROUP BY table_a.email
will return the total number of rows PER email address.
In codeigniter 3, we use
$this->db->group_by("table_a.email");

Related

Inner join query in codeigniter

code:
public function draft_post($idd)
{
$this->db->select('*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In this codes, I have two table i.e. registration and draft_registration. Now, What am I doing here I want to run inner join in Codeigniter. Now, What happening when I hit this query on phpmyadmin it shows wrong data i.e. I have two rows in draft_registration and one row in registration table but it always shows two table which is wrong and my query looks like when I was print as mention below:
SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'
So, How can I resolve this issue? Please help me.
Thank You
$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.
For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');
Specify column that you want to select. Or if you want select all column of your table, you can use :
SELECT registration.* with backticks `` on column name
Use the Below Code
public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Or you can use with objects
public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

Multiple join with one group by

I need to join and fetch all data from 3 tables and need to group it using a particular tables field name.Am using codeigniter and sql server
my query:
$this->db->select('table1.*,table2.*,table3.*');
$this->db->from('table1');
$this->db->join('table2','table1.id = table2.id');
$this->db->join('table3','table1.sid = table3.sid');
$this->db->group_by('table1.field_name');
Is it possible?
Am getting this error:
fieldname is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Generally, when you do a Group By in SQL Server, the columns you are permitted to include in your Select statement are limited to the columns in your Group By clause, plus a constant or an aggregate function. So you could do this:
Select Table1.field_name, Count(*) as ColumnCount
From Table1
Group By Table1.field_name
But you cannot include any other columns from Table1 unless they are in the Group BY.
use this in model
public function anyfunction($table3_id)
{
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table2.table2_id=table2.id', 'left');
$this->db->join('table3', 'table3.table1_id=table1.table1_id', 'left');
$this->db->where('table3.table1_id',$table3id);
$this->db->order_by('table3.field_name','asc');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result_array();
}
else
{
return false;
}
}
i hope it will work for you...

How to get count with joins in CodeIgniter

I am joining 3 tables and fetching data from database. But my problem is that I have to use 2 more tables to query to fetch a number of likes and numbers of comments on a post.
The query I am using is:
function GetHomeDeals($limit,$start)
{
$this->db->from('tbl_coupons');
$this->db->where('coupon_status', 'active');
$this->db->join('tbl_stores','tbl_stores.store_id=tbl_coupons.coupon_store');
$this->db->join('tbl_users','tbl_users.user_id=tbl_coupons.coupon_postedby');
$this->db->limit( $start,$limit);
$this->db->order_by("coupon_id", "desc");
$query = $this->db->get();
//echo $this->db->last_query();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
table structure of likes tables is:
like_id
like_by
like_on
table structure for comments
comment_id
comment_by
comment_on
comment
How can i add a count of likes and comments my function?
I just want total number of likes and count in result
like_on= coupon_id
comment_on=coupon_id
use this:
$query->num_rows();
example:
$query = $this->db->get('table');
$num = $query->num_rows();

mysql query with array returning first 3 coulmn without validation

I m joing table replies and table discussion where discussion have four row ie 1, 2,3,4
and i have foreign key to but it only return first 3 database row.. without even match
public function getReplies(array $id)
{
//problem in this query
$sql="SELECT r.*,u.*,d.* from tbl_replies as r
LEFT JOIN user as u ON r.user_id=u.user_id
LEFT JOIN tbl_discussion as d ON r.discussion_id = d.discussion_id where r.discussion_id in (". implode(',', array_map('intval', $id)) .")";
$query=$this->db->query($sql);
return $query->result();
}
this below function will send value to upper function
public function getId(){
$sql="select discussion_id from tbl_discussion";
$query= $this->db->query($sql);
return $query->result_array();
}

Combining 2 sql queries in codeigniter

I have two tables. voting_ip and country. I want to retrieve results from country table where open_id_fk (foreign key) of voting_ip table is equal to open_id(Primary Key) of country table. How to write sql query to combine these queries and return the result. I am using the below code in my codeigniter model to retrieve number of occurances of open_id_fk in voting_ip table.
public function mostvotedans()
{
$this->db->select('open_id_fk, COUNT(*) as total');
$this->db->group_by('open_id_fk');
$this->db->order_by('total', 'desc');
$query = $this->db->get('voting_ip', 5);
return $query;
$query = $this->db->query("SELECT * FROM country WHERE open_id_fk=open_id;");
return $query;
}
change it as following.
public function mostvotedans()
{
$this->db->select('c.open_id,COUNT(ip.open_id_fk) as total')->from('voting_ip ip');
$this->db->join('country c','c.open_id=ip.open_id_fk');
$this->db->group_by('ip.open_id_fk');
$this->db->order_by('total', 'desc');
$this->db->limit(5);
$query = $this->db->get();
return $query;
}
Use a join statement :
$query = $this->db->select('v.open_id_fk, COUNT(v.*) AS total, c.country_name')
->join('country AS c', 'c.open_id = v.open_id_fk')
->from('voting_ip AS v')
->group_by('v.open_id_fk')
->order_by('total', 'DESC')
->limit(5);
Should work, I put 'country_name' because I don't know your tables.

Categories