Welcome Guest, Not a member yet? Register   Sign In
Active Record Subqueries?
#1

[eluser]darkhouse[/eluser]
I can't see a way, and I'm betting it's not possible, to do a subquery using the active record functions, aside from just doing this:

Code:
$this->db->query("SELECT COUNT(*) AS total_count FROM (SELECT MAX(last_updated) FROM table) AS counter");

I was hoping to be able to use things like $this->db->where(), etc, as I have some conditions I need to check that will affect how I write the SQL.

Any suggestions, or should I just build my SQL string and use the query method?
#2

[eluser]gon[/eluser]
I would go with a string query.
It's as safe as active record, as long as you use the ? for substitutions.
#3

[eluser]darkhouse[/eluser]
Thanks, I did that and it's all good. Too bad AR doesn't support subqueries.
#4

[eluser]roj[/eluser]
I haven't tried it myself yet but I think that's what the FALSE option is for...

Code:
$this->db->select(("SELECT COUNT(*) AS total_count FROM (SELECT MAX(last_updated) FROM table) AS counter") FALSE);

It's in the guide anyway.....
#5

[eluser]darkhouse[/eluser]
Ah, thanks I should've just looked at the docs. I've already just built the SQL statement with some imploded arrays and moved on, but I'll definitely do this in the future. Thanks.
#6

[eluser]roj[/eluser]
No probs, like I said, haven't tried it myself so no guarantees....




Theme © iAndrew 2016 - Forum software by © MyBB