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

发布于 2024-07-10 19:24:54 字数 686 浏览 8 评论 0原文

假设有这样的东西:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心 2024-07-17 19:24:54

是的,您可以将 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 a fetchAll() method, but it doesn't take a SQL string, and it returns a Zend_Db_Table_Rowset object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文