使用Zend DB编写子查询

时间:2009-08-27 11:46:40

标签: sql mysql zend-framework

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

$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没有正确生成查询。谁能告诉我我错过了什么?

$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功能。

非常感谢任何帮助。

PJ

4 个答案:

答案 0 :(得分:32)

如果你愿意,你可以采取@ karim79所做的事情,并将你的子选择转换为$ this-> select()以及......

$subselect = $db->select()
->from('sle_instances', array(new Zend_Db_Expr('max(id)')))
->where('sle_id = s.id');

$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 = ($subselect)")
->order('i.name asc');

print($select);

//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

答案 1 :(得分:20)

此:

    $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"

答案 2 :(得分:3)

我有一个非常类似的问题,我发现这个查询可以很容易地编写如下:

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

这与人们在这里已经说过的完全相同。但我觉得它更容易阅读,因为子查询依赖性更明显。

答案 3 :(得分:1)

好问题!谢谢你这个。如果您在订单后尝试创建一个组,也可以使用这种语法与以下内容非常相似

$subquery = $this->_datawarehouse->select()
            ->from('revenueLog')
            ->where('Date '.$ReturnDate)
            ->order('Date DESC');

        $this->view->end = microtime();
        $format = new Zend_Db_Expr('DATE_FORMAT(`Date`,"%d-%m-%y")');
        $select = $this->_datawarehouse->select()
                ->from(array('subquery'=>$subquery))
                ->group('Client')
                ->group($format)
                ->order('Vertical ASC')
                ->order('Revenue DESC');

        echo $select->__ToString();
        $stmt = $this->_datawarehouse->query($select);
        $data = $stmt->fetchAll();

任何想知道$ ReturnDate的人都是基于用户输入的字符串,通常最终是“BETWEEN'date1'和'date2'”