如何使用Zend_Db快速创建复杂的选择查询?

时间:2008-12-13 01:55:31

标签: php mysql zend-framework zend-db-table

假设有类似的内容:

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

我可以将它填入变量并像$ db-> fetchAll($ select) - > toArray一样调用吸盘吗?

1 个答案:

答案 0 :(得分:3)

是的,您可以将SQL语句作为字符串传递给$db->fetchAll()方法。

您不需要在结果上调用toArray(),因为默认情况下结果已作为数组返回。

Zend_Db_Table类也有一个fetchAll()方法,但它不接受SQL字符串,并返回一个Zend_Db_Table_Rowset对象。