I have 3 mysql tables.
Table 1 user
id | name
Table 2 emails
id | email
Table 3 user_email
user_id | email_id
I have no experience in query multi tables.
Using codeigniter active record, i want to find out the user email address based on the user id, pls advise if the below code is correct ?
$CI->db->select('email');
$CI->db->from('emails');
$CI->db->where('id', $userid);
$CI->db->join('user_email', 'user_email.user_id = emails.id', 'left');
$query = $CI->db->get();
You have wrong where clause you need to compare user_id from your table ,you are comparing the id of email to the provided $user_id
$CI->db->select('email');
$CI->db->from('emails');
$CI->db->where('user_id', $userid);
$CI->db->join('user_email', 'user_email.user_id = emails.id', 'left');
$query = $CI->db->get();
A more useful way is to give aliases to your tables so the tables with same columns will not have any confusion
$CI->db->select('e.email');
$CI->db->from('emails e');
$CI->db->join('user_email ue', 'ue.user_id = e.id', 'left');
$CI->db->where('ue.user_id', $userid);
$query = $CI->db->get();
#m khalid 's answer is correct but I have created a dynamic function to achieve join with multiple tables. Check this out.
function join_records($table, $joins, $where = false, $select = '*', $orderBy = false, $direction = 'DESC'){
$CI->select($select);
$CI->from($table);
foreach($joins as $join){
$CI->join($join[0], $join[1], $join[2]);
}
if($where) $CI->where($where);
if($orderBy) $CI->order_by($orderBy, $direction);
$query = $CI->get();
return $query->result_array();
}
Applying your question to this.
$table = 'emails';
$joins[0][0] = 'user_email';
$joins[0][1] = 'user_email.user_id = emails.id';
$joins[0][2] = 'left';
$where['user_id'] = $userid;
You may add more join like $join1[0]..
If you need to select some specific column you can define in following manner
$select = 'table1.column1, table1.column2, table2.column1, table2.column2'
Or if you want all the columns put a *
$this->join_records($table, $joins, $where, $select);
You may also find it here.
Related
i need some codeigniter 3 help from you friends. its like 10 years ago when i did some more complex querys and my noobish JOIN-trys just gave me errors and questionmarks for hours.
lets say i have a mysql table covers
id, text, bgcolor_id, color_id
example : 1, "nice headline", 55, 88
and a table colors
id, value, name
example : 55, #FF0000, "red"
example : 88, #000000, "black"
how to "link" based on bgcolor_id, color_id in table covers
cover.bgcolor_id ->
color.value AS bgcolorvalue
color.name AS bgcolorname
cover.color_id ->
color.value AS colorvalue
color.name AS colorname
my codeigniter model
public function list(){
$query = $this->db->query('SELECT * FROM covers ORDER BY id DESC');
return $query->result_array();
}
public function get($id){
$query = $this->db->query('SELECT * FROM covers WHERE id = ' . $id);
return $query->row();
}
Join twice your colors table
select c.*,c1.name bgcolorname,
c1.value bgcolorvalue,
c2.name colorname,
c2.value colorvalue
from covers c
join colors c1 on c.bgcolor_id = c1.id
join colors c2 on c.color_id = c2.id
DEMO
I strongly recommend you to use the query builder Reference
You could to something like.
$cv = 'covers';
$cl1 = 'colors';
$cl2 = 'colors';
$get = array(
$cv.'.id',
$cv.'.text',
$cv.'.bgcolor_id',
$cv.'.color_id',
$cl1.'.value as bgcolorvalue',
$cl1.'.name as bgcolorname',
$cl1.'.value as colorvalue',
$cl1.'.name as colorname'
);
$this->db->select($get);
$this->db->from($cv);
$this->db->where($cv.'.id', 1);
$this->db->join($cl1, $cv.'.bgcolor_id = ' . $cl1.'.id');
$this->db->join($cl2, $cv.'.color_id = ' . $cl2.'.id');
$result = $this->db->get()->result();
let me know if this works
As i have understood your question right then you want to join on colors table twice for bgcolor_id and color_id, so here i am providing solution for your question let me know if it works or something wrong with the solutuion, thanks.
public function list(){
$query = $this->db->select("covers.*, bg.value AS bgcolorvalue bg.name AS
bgcolorname, colors.value AS colorvalue
colors.name AS colorname")
->join("colors", "colors.id = covers.color_id", "left")
->join("colors as bg", "bg.id = covers.bgcolor_id", "left")
->get("covers")->result();
return $query->result_array();
}
$array = array('classesID' => 6);
$this->db->select()->from($this->_table_name)->where($array)->order_by($this->_order_by);
$query = $this->db->get();
return $query->result();
I need to get the rows where the classesID is 6. Some rows contain 6,5,4.
so i need to use FIND_IN_SET query to retrive where the classesID is 6.
Please guide me
Thanks
I need to get the rows which has student id '4488' and message id '1418'
$id = 1418;
$student_id = 4488;
this->db->select('id, action_status, updated_on, student_ids');
$this->db->where('message_id', $id);
$this->db->where('find_in_set("'.$student_id.'", student_ids) <> 0');
$this->db->from('actions_history');
$query = $this->db->get();
It will return a query like
SELECT id, action_status, updated_on, student_ids FROM actions_history WHERE message_id = '1418' AND find_in_set("4488", student_ids) <>0
Try this
$array = array('classesID' => '5,6,7');
$this->db->select();
$this->db->from($this->_table_name);
$this->db->where("FIND_IN_SET('classesID',".$array['classesID'].")",null,false);
$this->db->order_by($this->_order_by);
$query = $this->db->get();
return $query->result();
You can write the query like this.
return $this->db->where('classesID' , '6')->order_by('column_name','desc')->get('table_name')->result_array();
if Need any help comment
For those who're looking for model (CI v4.x) based solution,
$modal->where("FIND_IN_SET('<string to find>', '<column name>')", null, false)->findAll();
In your case,
$modal->where("FIND_IN_SET('".$array['classesID']."', 'classesID')", null, false)->findAll();
I have 2 table users and user_request
user
id name
1 U1
2 U2
3 U3
4 U4
5 U5
user_request
id userid name status
1 1 U1 rejected
2 3 U3 rejected
I need list of users from user table but those users should not be there which are present in user_request table
So according to above example i need the following list of users
U2
U4
U5
From the user table i am using the following code to fetch the result
$query = $this->db->get('user');
return $query->result();
From the user_request table i am using the following code to fetch the result
$this->db->where('status','rejected');
$query = $this->db->get('user_request');
return $query->result();
Can anyone please help me to get the desired result?
Use a simple query using NOT IN:
SELECT name FROM user WHERE id NOT IN (SELECT userid FROM user_request)
SQLFIDDLE
UPDATE: For Codeigniter:
$this->db->select('*')->from('user');
$this->db->where('`id` NOT IN (SELECT `userid` FROM `user_request`)', NULL, FALSE);
You can do a join:
$this->db->select('name');
$this->db->from('user');
$this->db->join('user_request', 'user.id = user_request.userid');
$this->db->where('status !=','rejected');
return $this->db->get();
Fetch the id from first table, fetch the userid from second table. remove the common values from the 2 arrays and you will get the desired result
$this->db->select('id');
$query = $this->db->get('user');
$first = $query->result();
foreach ($first as $key)
{
$user_one = $key->id;
$first_array[] = $user_one;
}
$this->db->where('status','rejected');
$query_two = $this->db->get('user_request');
$two = $query_two->result();
foreach ($two as $value)
{
$userid_two = $value->userid;
$second_array[] = $userid_two;
}
$arr_1 = array_diff($first_array, $second_array);
$std = array();
foreach ($arr_1 as $value)
{
$this->db->where('id',$value);
$main_query = $this->db->get('user');
$main = $main_query->result();
$std = array_merge($std,$main);
}
return $std;
I have 3 mysql tables.
Table 1 user
id | name
Table 2 emails
id | email
Table 3 user_email
user_id | email_id
I have no experience in query multi tables.
Using codeigniter active record, i want to find out the user email address based on the user id, pls advise if the below code is correct ?
$CI->db->select('email');
$CI->db->from('emails');
$CI->db->where('id', $userid);
$CI->db->join('user_email', 'user_email.user_id = emails.id', 'left');
$query = $CI->db->get();
You have wrong where clause you need to compare user_id from your table ,you are comparing the id of email to the provided $user_id
$CI->db->select('email');
$CI->db->from('emails');
$CI->db->where('user_id', $userid);
$CI->db->join('user_email', 'user_email.user_id = emails.id', 'left');
$query = $CI->db->get();
A more useful way is to give aliases to your tables so the tables with same columns will not have any confusion
$CI->db->select('e.email');
$CI->db->from('emails e');
$CI->db->join('user_email ue', 'ue.user_id = e.id', 'left');
$CI->db->where('ue.user_id', $userid);
$query = $CI->db->get();
#m khalid 's answer is correct but I have created a dynamic function to achieve join with multiple tables. Check this out.
function join_records($table, $joins, $where = false, $select = '*', $orderBy = false, $direction = 'DESC'){
$CI->select($select);
$CI->from($table);
foreach($joins as $join){
$CI->join($join[0], $join[1], $join[2]);
}
if($where) $CI->where($where);
if($orderBy) $CI->order_by($orderBy, $direction);
$query = $CI->get();
return $query->result_array();
}
Applying your question to this.
$table = 'emails';
$joins[0][0] = 'user_email';
$joins[0][1] = 'user_email.user_id = emails.id';
$joins[0][2] = 'left';
$where['user_id'] = $userid;
You may add more join like $join1[0]..
If you need to select some specific column you can define in following manner
$select = 'table1.column1, table1.column2, table2.column1, table2.column2'
Or if you want all the columns put a *
$this->join_records($table, $joins, $where, $select);
You may also find it here.
I have the following tables:
tbl_users
===============
uid
username
password
gid
tbl_groups
===============
gid
name
type
I am trying to figure out how to use the sqlmapper in f3 to be able to query both tables where username equals $_POST["username"] and be able to get the group name and type as well. Is it possible to join like queries using this framework with sqlmapper?
I've been searching around and can't find any examples on that.
you can try to setup some virtual fields for this:
$mapper->group_name = 'select name from tbl_groups where tbl_groups.gid=tbl_users.gid';
$mapper->group_type = 'select type from tbl_groups where tbl_groups.gid=tbl_users.gid';
$mapper->load(array('uid = ?',123));
echo $mapper->group_name;
Here is the example with {VIEW}:
I have implemented here pagination with SQL Mapper using View view_user_list_with_referral
$dropInstantWinnerView = $this->db->exec("DROP VIEW IF EXISTS view_user_list_with_referral;");
$createInstantWinnerView = $this->db->exec("CREATE VIEW view_user_list_with_referral AS SELECT u.fb_id, fb_link, name, r.referred_by, u.created FROM users u LEFT OUTER JOIN referral r ON u.fb_id=r.joinee ");
$user = new \DB\SQL\Mapper($this->db,'view_user_list_with_referral');
$limit = 20;
$page = Pagination::findCurrentPage();
$order_condition = F3::get("PARAMS.order_condition");
$order_class= "";
if(!empty($order_condition)){
$cond = explode(":", $order_condition);
$option = array('order' => $cond[0].' '.$cond[1]);
if($cond[1]=='ASC'){
$order_condition = $cond[0].':DESC';
$order_class = ":DESC";
}else{
$order_condition = $cond[0].':ASC';
$order_class = ":ASC";
}
}else{
$option = array('order' => 'created DESC');
}
$subset = $user->paginate($page-1, $limit, null, $option);
$pages = new Pagination($subset['total'], $limit);
$pages->setTemplate("admin/pagebrowser.html");
F3::set('pagebrowser', $pages->serve());
//echo "<pre>";print_r($subset);exit;
F3::set('page', $page);
F3::set('order_condition', $order_condition);
F3::set('total_found_records', $user->count());
I hope it will save someones time :)