Welcome Guest, Not a member yet? Register   Sign In
MySQL IF, AS, and GROUP in Active Record?
#1

[eluser]skunkbad[/eluser]
My knowledge of complex MySQL is not as robust as I'd like it to be, but I've got a working query, and wondering if I can recreate it with Active Record, or what the best way to run it would be. The query simply checks if there is a record in table1 that hasn't been applied to a field in table2. I've simplified the table names and fields to make it easier to see:

Code:
SELECT  table1.*,
IF( COUNT( table2.something ) > 0, "false", "true" )
AS "unassigned"
FROM table1
LEFT JOIN table2
ON table1.whatever = table2.something
GROUP BY table1.whatever

No data will be passed to the query, so it's not necessary to worry about escaping anything. I'd just like to know if I can use active record or not.
#2

[eluser]toopay[/eluser]
use AR will generates 'ugly' code, like
Code:
$query = $this->db->select('table1.*, IF( COUNT( table2.something ) > 0, "false", "true" ) AS "unassigned"')
                  ->from('table1')
                  ->join('table2', 'table1.whatever = table2.something', 'left')
                  ->group_by('table1.whatever')
                  ->get();
If i have to do that, i'll process 'IF' statement outside the query instead put it on my sql statement, so i could have a cleaner code and better server performance.




Theme © iAndrew 2016 - Forum software by © MyBB