如何使用 Zend_Db 快速创建复杂的选择查询?
假设有这样的东西:
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 这样的傻瓜调用吗?
Let's say have 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 stuff it in a variable and call the sucker like $db->fetchAll($select)->toArray?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以将 SQL 语句作为字符串传递给
$db->fetchAll()
方法。您不需要对结果调用
toArray()
,因为默认情况下结果已作为数组返回。Zend_Db_Table
类还有一个fetchAll()
方法,但它不接受 SQL 字符串,并且返回一个Zend_Db_Table_Rowset
对象。Yes, you can pass a SQL statement as a string to the
$db->fetchAll()
method.You don't need to call
toArray()
on the result, because the result is already returned as an array by default.The
Zend_Db_Table
class also has afetchAll()
method, but it doesn't take a SQL string, and it returns aZend_Db_Table_Rowset
object.