Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 Complex UNION queries
#1

(This post was last modified: 11-11-2022, 12:24 AM by MurbiesWalto.)

I am new to Codeigniter 4 and trying to write SELECT UNION SQL Statements. According to the user guide in codeigniter 4, there is a second parameter in select() method that allows to write custom SQL Statements by setting it to false. Unfortunately it is not working. Can someone help omegle shagle voojio please? Is there any alternate solution?

$query = "SELECT acnt_id, acnt_opbal as sum_amount
                        FROM accounts
                            WHERE acnt_id = '" . $acnt_id . "'
                    UNION
                    SELECT txn_acnt_id_dr as acnt_id, SUM(txn_amount_dr) as sum_amount
                        FROM transactions
                            WHERE txn_acnt_id_dr = '" . $acnt_id . "'
                                AND txn_date < '" . $fdate . "'
                            GROUP BY txn_acnt_id_dr
                    UNION
                    SELECT txn_acnt_id_cr as acnt_id, -SUM(txn_amount_cr) as sum_amount
                        FROM transactions
                            WHERE txn_acnt_id_cr = '" . $acnt_id . "'
                                AND txn_date < '" . $fdate . "'
                            GROUP BY txn_acnt_id_cr";
$result = $this->builder()->select($query, false)->get()->getResult();
Reply
#2

I recommend you don't use QueryBuilder if you want to write SQL statements freely.
https://codeigniter4.github.io/CodeIgnit...eries.html
Don't forget escape the values.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB