How to use FIND_IN_SET in codeigniter query? - php

$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();

Related

CodeIgniter Active Records where in subquery

How do I write where_in statement a subquery using codeigniter active records ?
$query = $this->db->query("SELECT SUM(a.transaction_payment_amount) FROM
transaction_payment a WHERE a.transaction_link IN
(SELECT transaction_link FROM transaction WHERE transaction_type = '22'");
$result = $query->result();
Now how to convert the above query into CI active records ?
I have tried:
$this->db->select("SUM(a.transaction_payment_amount)");
$this->db->from('transaction_payment a');
$this->db->where_in('a.transaction_link', "SELECT transaction_link from transaction WHERE transaction_type = '22'");
$query = $this->db->get();
$result = $query->result();
But it doesn't work.
Try This
$this->db->where_in('a.transaction_link', "SELECT transaction_link from transaction WHERE transaction_type = '22'",false);
if you use false then it will remove single quotes from where_in condition
Sharmas Answer should do the job but if you wish fully supported Query Builder Methods you can try this
$strSubQuery = $this->db
->select("transaction_link")
->from("transaction")
->where("transaction_type",22)
->get_compiled_select();
$query = $this->db
->select("SUM(a.transaction_payment_amount)", false)
->from('transaction_payment a')
->where_in('a.transaction_link', $strSubQuery, false)
->get();
You can change your code as following solution.
Changes your query
$this->db->select("SUM(a.transaction_payment_amount)");
$this->db->from('transaction_payment a');
$this->db->where("a.transaction_link IN (SELECT transaction_link from transaction WHERE transaction_type = '22')", null, false);
//OR you can try other where condition if Sub query return null value than used this below query
$this->db->where("IF(SELECT transaction_link from transaction WHERE transaction_type = '22',a.transaction_link IN (SELECT transaction_link from transaction WHERE transaction_type = '22'), NULL)", null, false);
$query = $this->db->get();
$result = $query->result();
I hope this will helps you. Thanks!

Mysql get all rows but join if possible [duplicate]

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.

Convert to Codeigniter Query

How to write this query in codeigniter format?
SELECT rights_management.id, rights_management.tab_name FROM rights_management
WHERE rights_management.id NOT IN (SELECT r_m.tabid FROM r_m)
Thanks for the Help . .
You can write your sub query in CI using where clause
$this->db->select('rights_management.id, rights_management.tab_name');
$this->db->from('rights_management');
$this->db->where('`rights_management.id` NOT IN (SELECT r_m.tabid FROM `r_m`)', NULL, FALSE);
First of all you have to select r_m.tabid from table r_m and store it in array.
$this->db->select('r_m.tabid');
$this->db->from('r_m');
$query = $this->db->get();
$alb = $query->result_array();
Make it to an array
foreach($alb as $rs){
$not_need[]=$rs['tabid'];
}
After that fire the general active class using where_not_in.
$this->db->where_not_in('rights_management.id', $not_need);
$this->db->select('rights_management.id, rights_management.tab_name');
$this->db->from('rights_management');
$result= $this->db->get();
$this->db->query('SELECT rights_management.id, rights_management.tab_name FROM rights_management WHERE rights_management.id NOT IN (SELECT r_m.tabid FROM r_m)');

codeigniter active record left join

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.

want to set codeigniter join query

hi i ma try to join with two table.
this i my simple sql query
$sql="SELECT windows_users_image_upload.*, `windows_users_info`.`display`
FROM `windows_users_image_upload`,`windows_users_info`
WHERE `windows_users_info`.`user_id` = `windows_users_image_upload`.`user_id`
AND ".$field."=".$value;
its work fine but i want to set this to codeingter way
and try something like this.
$this->db->select("windows_users_image_upload.*,windows_users_info.display");
$this->db->from("windows_users_info,windows_users_image_upload");
$this->db->where("windows_users_image_upload.".$field,$value);
$this->db->limit($takeTuple, $startTuple);
$this->db->join("windows_users_image_upload,windows_users_image_upload.user_id = windows_users_info.user_id");
$result = $this->db->get()->result();
but its show me error
Message: Missing argument 2 for CI_DB_active_record::join()
how can i fix this.
thanks.
This is how you use a join in CodeIgniter:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
// Produces:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id1
You can see it in the User-Guide of CodeIgniter.
http://ellislab.com/codeigniter/user-guide/database/active_record.html#select
You have to remove de second table in from command:
Example:
$this->db->select('windows_users_image_upload.*,windows_users_info.display');
$this->db->from('windows_users_info');
$this->db->join("windows_users_image_upload","windows_users_image_upload.user_id = windows_users_info.user_id");
$this->db->where("windows_users_image_upload.".$field,$value);
$query = $this->db->get();

Categories