2016-08-03 61 views
1

我无法将我的查询放入zf2查询中,我可以适合简单的连接查询但我无法写入更复杂的子查询。 请帮我做到这一点。如何将复杂的mysql查询合并到zf2中

SELECT `created_date` FROM `salesmodule_discussed_topic` dt WHERE dt.`meeting_id` IN( 
SELECT ma.`meeting_id` FROM `salesmodule_meeting_agent` ma WHERE ma.`agent_id`=30547 
) 



public function getLastmeetingdate() { 
     $select = new Select(); 
     $select->from(array('dt' => 'salesmodule_discussed_topic')); 
     $select->columns(array('created_date')); 
     $select->where(array('ma.`agent_id` => 30547)); 
     $resultSet = $this->tableGateway->selectWith($select); 
     return $resultSet->buffer(); 
    } 

回答

-1
public function fetchAllSubQuery($id = '') { 
    $columns = array("*"); 
    $where = "main_booking_id IN (SELECT 
      b1.`main_booking_id` 
      FROM 
      `booking_sector` b1 
      WHERE b1.`connected_booking_id` = '$id' OR b1.`main_booking_id` = '$id') "; 
    $resultSet = $this->tableGateway->select(function (Select $select) use ($where,$columns) { 
      $select->where($where); 
      $select->columns($columns); 
     }); 
    $resultSet->buffer(); 
    // $resultSet->next(); 
    return $resultSet; 
} 
+1

'WHERE b1.connected_booking_id ='$ id'' MySql Injection – newage

2

前面的例子是不好的,因为有一个sql injections。您需要使用Select进行子查询。

public function getLastmeetingdate() { 
    $subQuery = new Select(); 
    $subQuery->from(array('ma' => 'salesmodule_meeting_agent')); 
    $subQuery->columns(array('meeting_id')); 
    $subQuery->where(array('ma.agent_id' => 30547)); 

    $select = new Select(); 
    $select->from(array('dt' => 'salesmodule_discussed_topic')); 
    $select->columns(array('created_date')); 
    $select->where->in('dt.meeting_id', $subQuery); 

    return $this->tableGateway->selectWith($select); 
}