i am little confused in wririting SQL query for joining muthiple tables.
I have perfectly write that query in codeigniter as folows
$this->db->select('*');
$this->db->from('subscription_payment');
$this->db->join('user', 'user.user_id = subscription_payment.user_id');
$this->db->join('subscription', 'subscription.subscription_id =
subscription_payment.subscription_id');
$this->db->where('subscription_payment.subscription_payment_id',$sid);
$query = $this->db->get();
$result= $query->result_array();
Help me to convert this to SQL query
Using
echo $this->db->last_query();
will produce
select * from some_table...
And this is it. Next time you can convert any query you want with easy.
select *
from subscription_payment
join user
on user.user_id = subscription_payment.user_id
join subscription
on subscription.subscription_id = subscription_payment.subscription_id
where subscription_payment.subscription_payment_id = $sid
SELECT *
FROM subscription_payment
JOIN user ON user.user_id = subscription_payment.user_id
JOIN subscription ON subscription.subscription_id = subscription_payment.subscription_id
WHERE subscription_payment.subscription_payment_id = $sid
;
How to do UNION query with PHP CodeIgniter framework's active record query format?
CodeIgniter's ActiveRecord doesn't support UNION, so you would just write your query and use the ActiveRecord's query method.
$this->db->query('SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2');
By doing union using last_query(), it may hamper performance of application. Because for single union it would require to execute 3 queries. i.e for "n" union "n+1" queries. It won't much affect for 1-2 query union. But it will give problem if union of many queries or tables having large data.
This link will help you a lot: active record subqueries
We can combine active record with manual queries.
Example:
// #1 SubQueries no.1 -------------------------------------------
$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
$subQuery1 = $this->db->_compile_select();
$this->db->_reset_select();
// #2 SubQueries no.2 -------------------------------------------
$this->db->select('title, content, date');
$this->db->from('mytable2');
$query = $this->db->get();
$subQuery2 = $this->db->_compile_select();
$this->db->_reset_select();
// #3 Union with Simple Manual Queries --------------------------
$this->db->query("select * from ($subQuery1 UNION $subQuery2) as unionTable");
// #3 (alternative) Union with another Active Record ------------
$this->db->from("($subQuery1 UNION $subQuery2)");
$this->db->get();
This is a quick and dirty method I once used
// Query #1
$this->db->select('title, content, date');
$this->db->from('mytable1');
$query1 = $this->db->get()->result();
// Query #2
$this->db->select('title, content, date');
$this->db->from('mytable2');
$query2 = $this->db->get()->result();
// Merge both query results
$query = array_merge($query1, $query2);
Not my finest work, but it solved my problem.
note: I didn't need to order the result.
You may use the following method to get the SQL statement in the model:
$this->db->select('DISTINCT(user_id)');
$this->db->from('users_master');
$this->db->where('role_id', '1');
$subquery = $this->db->_compile_select();
$this->db->_reset_select();
This way the SQL statement will be in the $subquery variable, without actually executing it.
You have asked this question a long time ago, so maybe you have already got the answer. if not, this process may do the trick.
by modifying somnath huluks answer, i add these following variable and functions to DB_Active_rec class as follows:
class DB_Active_records extends CI_DB_Driver
{
....
var $unions;
....
public function union_push($table = '')
{
if ($table != '')
{
$this->_track_aliases($table);
$this->from($table);
}
$sql = $this->_compile_select();
array_push($this->unions, $sql);
$this->_reset_select();
}
public function union_flush()
{
$this->unions = array();
}
public function union()
{
$sql = '('.implode(') union (', $this->unions).')';
$result = $this->query($sql);
$this->union_flush();
return $result;
}
public function union_all()
{
$sql = '('.implode(') union all (', $this->unions).')';
$result = $this->query($sql);
$this->union_flush();
return $result;
}
}
therefore you can virtually use unions without dependencies to db_driver.
to use union with this method, you simply make regular active record queries, but calling union_push instead of get.
note: you have to ensure your queries have matching columns like regular unions
example:
$this->db->select('l.tpid, l.lesson, l.lesson_type, l.content, l.file');
$this->db->where(array('l.requirement' => 0));
$this->db->union_push('lessons l');
$this->db->select('l.tpid, l.lesson, l.lesson_type, l.content, l.file');
$this->db->from('lessons l');
$this->db->join('scores s', 'l.requirement = s.lid');
$this->db->union_push();
$query = $this->db->union_all();
return $query->result_array();
would produce:
(SELECT `l`.`tpid`, `l`.`lesson`, `l`.`lesson_type`, `l`.`content`, `l`.`file`
FROM `lessons` l
WHERE `l`.`requirement`=0)
union all
(SELECT `l`.`tpid`, `l`.`lesson`, `l`.`lesson_type`, `l`.`content`, `l`.`file`
FROM `lessons` l
JOIN `scores` s ON `l`.`requirement`=`s`.`lid`)
I found this library, which worked nicely for me to add UNION in an ActiveRecord style:
https://github.com/NTICompass/CodeIgniter-Subqueries
BUT I had to grab the get_compiled_select() method from the dev branch of CodeIgniter first (available here: https://github.com/EllisLab/CodeIgniter/blob/develop/system/database/DB_query_builder.php -- DB_query_builder will be replacing DB_active_rec). Presumably this method will be available in a future production release of CodeIgniter.
Once I added that method to DB_active_rec.php in system/database it worked like a charm. (I didn't want to use the dev version of CodeIgniter as this is a production app.)
try this one
function get_merged_result($ids){
$this->db->select("column");
$this->db->distinct();
$this->db->from("table_name");
$this->db->where_in("id",$model_ids);
$this->db->get();
$query1 = $this->db->last_query();
$this->db->select("column2 as column");
$this->db->distinct();
$this->db->from("table_name");
$this->db->where_in("id",$model_ids);
$this->db->get();
$query2 = $this->db->last_query();
$query = $this->db->query($query1." UNION ".$query2);
return $query->result();
}
This is solution I am using:
$union_queries = array();
$tables = array('table1','table2'); //As much as you need
foreach($tables as $table){
$this->db->select(" {$table}.row1,
{$table}.row2,
{$table}.row3");
$this->db->from($table);
//I have additional join too (removed from this example)
$this->db->where('row4',1);
$union_queries[] = $this->db->get_compiled_select();
}
$union_query = join(' UNION ALL ',$union_queries); // I use UNION ALL
$union_query .= " ORDER BY row1 DESC LIMIT 0,10";
$query = $this->db->query($union_query);
bwisn's answer is better than all and will work but not good in performance because it will execute sub queries first.
get_compiled_select does not run query; it just compiles it for later run so is faster
try this one
$this->db->select('title, content, date');
$this->db->where('condition',value);
$query1= get_compiled_select("table1",FALSE);
$this->db->reset_query();
$this->db->select('title, content, date');
$this->db->where('condition',value);
$query2= get_compiled_select("table2",FALSE);
$this->db->reset_query();
$query = $this->db->query("$query1 UNION $query2");
Here's a solution I created:
$query1 = $this->db->get('Example_Table1');
$join1 = $this->db->last_query();
$query2 = $this->db->get('Example_Table2');
$join2 = $this->db->last_query();
$union_query = $this->db->query($join1.' UNION '.$join2.' ORDER BY column1,column2);
$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'am new on postgresql, i have some query like this :
SELECT * FROM "trx_mutualfund_trade" RIGHT JOIN "trx_mutualfund_nav" ON "trx_mutualfund_trade"."mtr_trade_time"::timestamp::date = trx_mutualfund_nav.mna_efective_date ....
and i try to write CI code like this :
$this->db->select('*');
$this->db->from('mutualfund_trade');
$this->db->join('mutualfund_nav', 'mutualfund_trade.mtr_trade_time::timestamp::date = mutualfund_nav.mna_efective_date', 'right');
but that CI Code produces query like this :
SELECT * FROM "trx_mutualfund_trade" RIGHT JOIN "trx_mutualfund_nav" ON "trx_mutualfund_trade"."mtr_trade_time"::"timestamp::date" = mutualfund_nav.mna_efective_date...
So that can't run because there is an doublequote on ::"timestamp::date"
Anyone can help me to show me how to write the CI code corectly?
thanks
Use this:
$query = $this->db->query("SELECT * FROM trx_mutualfund_trade RIGHT JOIN trx_mutualfund_nav ON trx_mutualfund_trade.mtr_trade_time::timestamp::date = trx_mutualfund_nav.mna_efective_date");
$result = $query->result_array();
return $result
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();