Zend_Db_Select:在子选择上LEFT JOIN

Jac*_*cco 3 subquery left-join zend-db zend-db-select

我有一个查询,LEFT JOIN在子选择上执行.此查询在高负载环境中运行,并在设置要求内执行.查询(高度简化)看起来像:

SELECT
  table_A.pKey
, table_A.uKey
, table_A.aaa
, table_B.bbb
, alias_C.ccc
, alias_C.ddd
FROM table_A
INNER JOIN table_B ON table_A.pKey = table_B.pKey
LEFT JOIN (

    SELECT
      table_X.pKey
    , table_X.ccc
    , table_Y.ddd
    FROM table_X
    INNER JOIN table_Y ON table_X.pKey = table_Y.pKey

  ) AS alias_C ON table_A.uKey = alias_C.pKey;
Run Code Online (Sandbox Code Playgroud)

(由于各种原因,不可能将子选择重写为(直接)LEFT JOIN).

现在,我无法LEFT JOIN on subselect与之合作Zend_Db_Select.我已经尝试了我能想到的一切,但它不起作用.


所以我的问题是:

  • 是不是可以像上面描述的那样进行查询 Zend_Db_Select
  • 在Zend Framework中使用它需要什么语法?

Jak*_*ček 8

我认为它应该像这样工作:

$subselect = $db->select->from(array('x' => 'table_X'), array('x.pKey', 'x.ccc', 'y.ddd'), 'dbname')
                        ->join(array('Y' => 'table_Y'), 'x.pkey = y.pkey', array(), 'dbname');

$select = $db->select->from(array('a' => 'table_A'), array(/*needed columns*/), 'dbname')
                     ->join(array('b' => 'table_B'), 'a.pkey = b.pkey', array(), 'dbname')
                     ->joinLeft(array('c' => new Zend_Db_Expr('('.$subselect.')'), 'c.pkey = a.ukey', array())
Run Code Online (Sandbox Code Playgroud)

我没试过,但我相信它会奏效.