【问题标题】:How to execute union query in zf2 where i m using tablegateway?如何在 zf2 中使用 tablegateway 执行联合查询?
【发布时间】:2014-07-07 22:18:59
【问题描述】:

如何执行下面的查询

select * from table1 union select * from table2

在 zend-framework2 中我在哪里使用 tablegateway?在zf2的文档中,他们没有给出关于联合查询的任何细节。

【问题讨论】:

    标签: postgresql zend-framework2 union


    【解决方案1】:

    试试-

    $select1 = new Select('table1');
    [.... rest of the code ....]
    
    
    $select2 = new Select('table2');
    [.... rest of the code ....]
    
    $select1->combine($select2); //This will create the required SQL union statement.
    

    要获得两个表的计数,您必须使用一些 SQL 而不是 tableGateway -

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

    $resultSet 给出数据。

    $total_records 给出总数。记录。

    【讨论】:

    • 感谢您的帮助.. 基于某些条件(使用 where 语句),我将在 table1[eg:10] 中计数,在 table2[eg:20]..现在我加入了两个表加入表格后不清楚如何获得计数..
    • 更新了答案。请检查。
    • 我现在可以得到计数。我想包含我在 zend\db\sql\where 中编写的“where 条件”..如何将 $where 附加到 $statement
    • 仍然找不到将$where 转换为字符串并追加的方法。我想现在,您将不得不在原始 SQL 中编写条件并追加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多