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.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分