I applied the join query, but it seems to be not working. It's not selecting any data. When print_r the row_data is empty, but when I print_r $q it shows all the data,
public function getUserdata($id) {
$id=$id;
$query=$this->db->select('post_status.status_image')
->where('user_data.id',$id)
->from('user')
->join('user_data', 'user.id = user_data.id')
->join('post_status', 'user.id = post_status.user_id')
->get();
echo "<pre>";
print_r($query);
exit();
$q= $query->result_array();
return $q;
}
use where after join like this
$query=$this->db->select('post_status.status_image')
->from('user')
->join('user_data', 'user.id = user_data.id')
->join('post_status', 'user.id = post_status.user_id')
->where('user_data.id',$id)
->get();
also you can use toSql() instead of get() to check your raw query in phpMyAdmin. ;)
Related
Please help me how can I get only response where activities.activity_id = response.activity_id? here is my CI_model
public function get_response(){
$this->db->select('*');
$this->db->from('response');
$id = $this->session->userdata('id');
$this->db->where_in('response.user_id', $id);
$this->db->join('activities', 'response.activity_id = activities.activity_id');
$this->db->join('users', 'users.id = response.user_id');
$result = $this->db->get()->result_array();
return $result;
}
My Activities table
My Response Table
My users table
Try with this:
$id = $this->session->userdata('id');
$this->db->select('a.*, b.*, c.*');
$this->db->join('activities b', 'a.activity_id = b.activity_id');
$this->db->join('users c', 'c.id = a.user_id');
$this->db->where_in('a.user_id', $id);
$result = $this->db->get('response a')->result_array();
return $result;
You need to add aliases to the tables in order to build a more simplified and ordered query, in this case the aliases are a, b and c.
UPDATE: I fix a get method incorrectly writed when copy your code.
Check the query snippet shared here:
https://extendsclass.com/mysql/526c246
I hope to be helpful
I have query which gets each user's record from blacklist table and relations to them from rules table.
It looks like that:
$result1 = DB::table('blacklist')
->join('rules', 'blacklist.rule_id', '=', 'rules.id')
->select('blacklist.*', 'rules.clicks', 'rules.minutes')
->groupBy('blacklist.address')
->where('blacklist.user_id', JWTAuth::user()->id)
->get();
However I set field blacklist.rule_id as CAN BE NULL. Above query doesn't get records which have blacklist.rule_id == null(only these with values).
I have second query for that purpose:
$result2 = DB::table('blacklist')
->select('blacklist.*')
->groupBy('blacklist.address')
->where('blacklist.user_id', JWTAuth::user()->id)
->where('blacklist.rule_id', null)
->get();
But how to do it in one query?
It is actually an leftJoin instead of join:
$result1 = DB::table('blacklist')
->leftJoin('rules', 'blacklist.rule_id', '=', 'rules.id')
->select('blacklist.*', 'rules.clicks', 'rules.minutes')
->groupBy('blacklist.address')
->where('blacklist.user_id', JWTAuth::user()->id)
->get();
try this
$result2 = DB::table('blacklist')
->select('blacklist.*')
->groupBy('blacklist.address')
->where(function ($result2) {
$result2->where('blacklist.user_id', JWTAuth::user()->id)
->orWhereNull('blacklist.user_id');
}
->get();
I am attempting to create a query to get a single blog post from my blog table and at the same time get the information from that user on my users' table which was ok using Join but now I want to count the total comments of that blog post as total so that would be three tables to query blog, users and comments
But below code display 3 blog entry with the same content, and where to place the COUNT(*) as total for comment table, any suggestion would be great!
public function get_entry(){
$id = $this->input->post('ID', true);
$this->db->select('*, u.ID');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT');
$result = $this->db->get();
if($result->num_rows() > 0){
return $result->result_array();
}else{
return false;
}
}
Count the comment id and group the query by blog id.
public function get_entry(){
$id = $this->input->post('ID', true);
$this->db->select('u.*, a.*, count(b.ID) as total');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT')
->group_by('u.ID');
$result = $this->db->get();
if($result->num_rows() > 0){
return $result->result_array();
}else{
return false;
}
}
Try using this, you can change the query according to your requirement:-
$usr_flds = "count(u.ID) as count_rows";
$this->db->select('usr_flds');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT');
$result = $this->db->get();
return $res->num_rows();
try this.i hope it will work for you.
$result = $this->db->get();
$count = $result->get()->num_rows();
return [$result, $count];
I am using multiple joins but got stuck in this. I am using join of 3 tables but it fetches values of only 2 tables not 3rd one. Here my model query is:
public function seller_products()
{
$this->db->select('*')->select('wc_seller_products.id')->from('wc_seller_products')
->join('wc_seller', 'wc_seller.id = wc_seller_products.seller_id', 'LEFT')
->join('wc_seller_info', 'wc_seller_info.id = wc_seller_products.seller_id', 'LEFT');
$query = $this->db->get();
return $query;
}
It doesn't fetch values of wc_seller table .... please help
public function seller_products()
{
$this->db->select('wc_seller_products.*,wc_seller.*,wc_seller_info.*');
$this->db->from('wc_seller_products');
$this->db->join('wc_seller', 'wc_seller.id = wc_seller_products.seller_id');
$this->db->join('wc_seller_info', 'wc_seller_info.id = wc_seller_products.seller_id');
$query = $this->db->get();
return $query;
}
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.