【问题标题】:Select Columns to return when joining a table with Zend\Db\Sql使用 Zend\Db\Sql 连接表时选择要返回的列
【发布时间】:2015-09-08 18:33:06
【问题描述】:

我只想返回一列(per.cd_acao),所以我尝试了类似的方法:

$select->from(array('act' => 'tb_acao'))
           ->join(array('per' => 'tb_perfil_acao'),
                'per.cd_acao = act.cd_acao',
                array('cd_acao'),
                $select::JOIN_INNER
            );

但这会产生如下查询字符串: SELECT "act".*, "per"."cd_acao" AS "cd_acao" FROM "tb_acao" AS "act" INNER JOIN "tb_perfil_acao" AS "per" ON "per"."cd_acao" = "act"."cd_acao " WHERE "per"."sq_perfil" = '89'

当我不需要时,它会从第一个表中获取所有列。我在这里错过了什么?

更新

总结:当我没有在选择对象中通知“列”时,它默认将所有列返回给我。但是当我加入时,我不希望第一个表返回任何列。

【问题讨论】:

    标签: zend-framework2 zend-db


    【解决方案1】:

    一个空数组就足够了

    $select->from(array('act' => 'tb_acao'))
               ->columns(array())
               ->join(array('per' => 'tb_perfil_acao'),
                    'per.cd_acao = act.cd_acao',
                    array('cd_acao'),
                    $select::JOIN_INNER
                );
    

    【讨论】:

      【解决方案2】:

      试试这个查询

      use Zend\Db\Sql\Sql;
      

      protected $tableGateway;
      public $adapter;
      
      public function __construct(TableGateway $tableGateway)
      {
          $this->tableGateway = $tableGateway;
      }
      
      publice function getData(){
          $adapter = $this->tableGateway->getAdapter();
          $sql = new Sql($adapter);
          $select = $sql->select();
          $select->from('tb_acao')
             ->join('per'),
                  'per.cd_acao = act.cd_acao',
                  array('cd_acao'),
              );
          $statement = $sql->prepareStatementForSqlObject($select);
          $results = $statement->execute();
      
          return $results;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多