使用 Zend DB 编写子查询

Writing a subquery using Zend DB(使用 Zend DB 编写子查询)
本文介绍了使用 Zend DB 编写子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将下面的 SQL 转换为 Zend Db 查询时遇到了一些问题.

I am having some problems turning the SQL below into a Zend Db query.

$select = ' SELECT s.id, i.id as instance_id, i.reference, i.name, i.sic_code, i.start_date
                FROM sles s
                JOIN sle_instances i
                ON s.id = i.sle_id
                WHERE i.id = (  SELECT MAX(id) 
                                FROM sle_instances 
                                WHERE sle_id = s.id
                                )
                ORDER BY i.name ASC';

我已经了解了之前的代码 - 但是 Zend Db 没有正确生成查询.谁能告诉我我错过了什么??

I have got as far as the code before - but Zend Db isn't producing the query correctly. Can any one show me what I missing??

$select = $db->select() ->from('sles', array(   'id',
                                                'instance_id'   => 'sle_instances.id',                                                                                          
                                                'reference'     => 'sle_instances.reference',           
                                                'name'          => 'sle_instances.name',
                                                'sic_code'      => 'sle_instances.sic_code',
                                                'start_date'    => 'sle_instances.start_date'
                                             )
                              )
                        ->join('sle_instances', 'sles.id = sle_instances.sle_id')
                        ->where('sles.id = (SELECT MAX(id) FROM sle_instances WHERE sle_id = sles.id)')
                        ->order('sle_instances.name ASC');

顺便说一下,SQL 确实有效.我正在使用 Zend Db 重写它,因为我希望使用 Zend Paginator 功能.

The SQL does work by the way. I am rewriting it using Zend Db as I wish to use the Zend Paginator functionality.

非常感谢任何帮助.

PJ

推荐答案

这个:

    $select = $db->select()->from(array("s" => "sles"), array("s.id","i.id as instanceid","i.reference","i.name","i.sic_code","i.start_date"))
                                 ->join(array('i' => "sle_instances"),"s.id = i.sle_id",array())
                                 ->where("i.id = (select max(id) from sle_instances where sle_id = s.id)")
                                 ->order('i.name asc');

给出这个:

"SELECT `s`.`id`, `i`.`id` AS `instanceid`, `i`.`reference`, `i`.`name`, `i`.`sic_code`, `i`.`start_date` FROM `sles` AS `s`
 INNER JOIN `sle_instances` AS `i` ON s.id = i.sle_id WHERE (i.id = (select max(id) from sle_instances where sle_id = s.id)) ORDER BY `i`.`name` asc"

这篇关于使用 Zend DB 编写子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)