dqyy38265 2014-07-07 14:03
浏览 43
已采纳

两个表的联合和计数与条件

I have to write the following query in zend-framework2

select count(*) from  
(
    select * from table1
    UNION
    select * from table2
)
as a
where col1 = condition1 and col2 = condition2;

I have done union of two tables using -

$select1 = new Select('table1');
$select2 = new Select("table2");
$select1->combine($select2);

I don't know how to give an alias after doing the union of two tables and how to get the count of data.

  • 写回答

1条回答 默认 最新

  • dsjpik057730 2014-07-07 14:09
    关注

    After $select1->combine($select2); -

    $sql = new Sql($this->tableGateway->adapter);
    $select_string = $sql->getSqlStringForSqlObject($select1);
    
    $sql_string = 'SELECT * FROM (' . $select_string . ') AS select_union WHERE col1 = condition1 and col2 = condition2';
    $statement = $this->tableGateway->adapter->createStatement($sql_string);
    $resultSet = $statement->execute();
    $total_records = count($resultSet);
    

    $resultSet gives data.

    $total_records gives total no. of records.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?