【问题标题】:Union of two tables in Zend 2Zend 2中两个表的并集
【发布时间】:2018-10-07 04:01:57
【问题描述】:

我想用 zf2 中的 where 子句合并两个表:-

table1 app_followers
表 2 app_users
条件可以是任何东西
并按更新日期订购。

请让我知道 zend 2 的查询。

谢谢..

【问题讨论】:

    标签: mysql zend-framework zend-framework2 zend-db zend-framework3


    【解决方案1】:

    使用 UNIONZF2

    使用ZF2专用类Combine Zend\Db\Sql\Combine

        new Combine(
         [
          $select1,
          $select2,
          $select3,
           ...
         ]
        )
    

    使用 combine 的详细示例如下:

    $select1 = $sql->select('java');
    $select2 = $sql->select('dotnet');
    $select1->combine($select2);
    
    $select3 = $sql->select('android');
    
    $selectall3 = $sql->select();
    $selectall3->from(array('sel1and2' => $select1));
    $selectall3->combine($select3);
    
    $select4 = $sql->select('network');
    
    $selectall4 = $sql->select();
    $selectall4->from(array('sel1and2and3' => $selectall3));
    $selectall4->combine($select4);
    
    $select5 = $sql->select('dmining');
    
    $selectall5 = $sql->select();
    $selectall5->from(array('sel1and2and3and4' => $selectall4));
    $selectall5->combine($select5);
    

    相当于普通的对 UNION 的 SQL 查询:

    SELECT * FROM java 
    UNION SELECT * from dotnet 
    UNION SELECT * from android 
    UNION SELECT * from network;
    UNION SELECT * from dmining;
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      我想做一个类似的任务,并花了很多时间来弄清楚如何以正确的方式完成它。

      Laminas\Db\Sql\Combine 的想法非常好,但是您不能将排序应用于此对象,因此在这种情况下它是无用的。

      最后,我得到了下一个代码:

      $skill = $sql->select('skill');
      $language = $sql->select('language');
      $location = $sql->select('location');
      $occupation = $sql->select('occupation');
      
      $skill->combine($language);
      $language->combine($location);
      $location->combine($occupation);
      
      $combined = (new Laminas\Db\Sql\Select())
          ->from(['sub' => $skill])
          ->order(['updated_date ASC']);
      

      但是,括号有点乱。如果对你有问题,请在 Github 上查看这个comment,但在 MySQL 上的 id 无所谓,不确定其他数据库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        • 2017-01-08
        • 1970-01-01
        • 1970-01-01
        • 2021-01-03
        • 1970-01-01
        相关资源
        最近更新 更多