如何使用 Zend 构建具有 DISTINCT 特定列的选择?
我的网站使用 Zend Framework,我想从 PostgreSQL 数据库检索一些数据。
我有一个这样的请求:
SELECT DISTINCT ON(e.id) e.*, f.*, g.* FROM e, f, g
WHERE e.id = f.id_e AND f.id = g.id_f
此请求运行良好,但我不知道如何使用 Zend 转换 DISTINCT ON(e.id) 。 看来我可以获得不同的行,但没有不同的列。
$select->distinct()->from("e")->join("f", "e.id = f.id_e")
->join("g", "f.id = g.id_f");
关于如何使用不同的列进行选择有什么想法吗?
感谢您的帮助
I'm using Zend Framework for my website and I'd like to retrieve some data from my PostgreSQL database.
I have a request like :
SELECT DISTINCT ON(e.id) e.*, f.*, g.* FROM e, f, g
WHERE e.id = f.id_e AND f.id = g.id_f
This request works well but I don't know how to convert the DISTINCT ON(e.id) with Zend.
It seems that I can get DISTINCT rows but no distinct columns.
$select->distinct()->from("e")->join("f", "e.id = f.id_e")
->join("g", "f.id = g.id_f");
Any idea on how to make a select with distinct column ?
Thanks for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能无法使用 Zend Framework 执行此操作,因为
distinct on
不是 SQL 标准的一部分(Postgres 文档中的页面末尾)。尽管 Postgres 支持它,但我认为它不是 Zend Framework 的一部分,因为理论上您可以配置另一个不提供支持的数据库连接。如果您事先知道您正在为特定数据库(本例中为 Postgres)进行开发,则可以使用手动编写的语句。您将在查询中获得更大的灵活性和更好的性能,但代价是不再能够切换数据库。
然后,您将为 Postgres 实例化一个 Zend_Db_Apdapter。有多种方法可用于获取 SQL 查询的结果,这些方法在框架文档中从 读取查询结果。如果您选择走这条路,我建议您创建 Zend_Db_Adapter_Pgsql 类自己的子类。这是为了能够转换数据类型并在出现错误时抛出异常,而不是返回不明确的
null
值并隐藏错误原因。You probably can't do this with Zend Framework since
distinct on
is not part of the SQL standard (end of page in Postgres documentation). Although Postgres supports it, I would assume its not part of Zend Framework because you could in theory configure another database connection which does not offer support.If you know in advance that you're developing for a specific database (Postgres in this case), you could use manually written statements instead. You'll gain more flexibility within the queries and better performance at the cost of no longer being able to switch databases.
You would then instantiate a Zend_Db_Apdapter for Postgres. There a various methods available to get results for SQL queries which are described in the frameworks documentation starting at section Reading Query Results. If you choose to go this route I'd recommend to create an own subclass of the Zend_Db_Adapter_Pgsql class. This is to be able to convert data types and throw exceptions in case of errors instead of returning ambiguous
null
values and hiding error causes.