How can I quickly create a complex select query using Zend_Db?
Let's say there is something like:
SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM (
SELECT max(energy_produced) AS energy_produced, mid(timestamp, 1, 10) AS timestamp1 FROM tbl_energy
WHERE site_id = 1366 AND mid(timestamp, 1, 10) >= DATE_SUB(curdate(),INTERVAL 1 day)
group by mid(timestamp1, 1, 10)) AS e1
INNER JOIN (
SELECT max(energy_consumed) AS energy_consumed, mid(timestamp, 1, 10) AS timestamp2 FROM tbl_energy
WHERE site_id = 1366 AND mid(timestamp, 1, 10) >= DATE_SUB(curdate(),INTERVAL 1 day)
group by mid(timestamp2, 1, 10)) AS e2
ON e1.timestamp1 = e2.timestamp2
Can I just write it in a variable and call the sucker like $ db-> fetchAll ($ select) -> toArray?
+1
source to share
1 answer
Yes, you can pass the SQL statement as a string to the method $db->fetchAll()
.
You don't need to call toArray()
on the result, because the result is already returned as a default array.
The class Zend_Db_Table
also has a method fetchAll()
, but it does not accept an SQL string and returns an object Zend_Db_Table_Rowset
.
+3
source to share