Writing a subquery using Zend DB

前端 未结 4 1889
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 07:08

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         


        
相关标签:
4条回答
  • 2020-12-03 07:26

    Great question! Thank you for this one. Also wanted to throw out that if you are trying to do a group after an order, you can also use this syntax by something very similar to the following

    $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();
    

    for anyone wondering $ReturnDate is a string based on a user input, which usually ends up being a "BETWEEN 'date1' AND 'date2'"

    0 讨论(0)
  • 2020-12-03 07:27

    I had a very similar problem and I found that this query can be easily written as follows:

    $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);
    

    It is exactly the same as people has already stated here. But I felt it is a little easier to read since the sub-query dependencies are more evident.

    0 讨论(0)
  • 2020-12-03 07:40

    This:

        $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');
    

    Gives this:

    "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"
    
    0 讨论(0)
  • 2020-12-03 07:41

    if you want, you can take what @karim79 did and turn your subselect into a $this->select() as well...

    $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
    
    0 讨论(0)
提交回复
热议问题