Welcome Guest, Not a member yet? Register   Sign In
Need help converting SQL to ActiveRecord?
#1

Can anyone assist in converting this SQL query into ActiveRecord codeigniter?

SELECT t.idkas, t.tglkas, (
SELECT (SUM(kredit))-(SUM(debet))
FROM kaskecil t2
WHERE t2.idkas <= t.idkas
) AS total
FROM kaskecil t
Reply
#2

(This post was last modified: 07-13-2023, 05:32 AM by brabus.)

I dont is it works:

Hey ChatGPT, convert this sql query to codeigniter 3 active record:

Code:
SELECT t.idkas, t.tglkas, (
SELECT (SUM(kredit))-(SUM(debet))
FROM kaskecil t2
WHERE t2.idkas <= t.idkas
) AS total
FROM kaskecil t

ChatGPT
You can convert the SQL query to CodeIgniter 3 Active Record format using the following code:


PHP Code:
$this->db->select('t.idkas, t.tglkas');
$subQuery $this->db->select('(SUM(kredit)) - (SUM(debet))'false)
    ->from('kaskecil t2')
    ->where('t2.idkas <= t.idkas'false)
    ->get_compiled_select();
$this->db->select("($subQuery) AS total"false);
$this->db->from('kaskecil t');
$query $this->db->get(); 

This code will perform the same query as your SQL query using CodeIgniter 3 Active Record. The subquery is created using the get_compiled_select() method to generate the subquery string. The resulting subquery is then used as a select expression in the main query. Finally, the get() method is called to execute the query and retrieve the results.

Just start to chat with ChatGPT it will be your friend/mentor, even if he we will not help youu 100%, he can try to help find the path...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB