3

我有一个查询,它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;

(由于各种原因,不可能将子选择重写为(直接)左连接)。

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


所以我的问题是:

  • 是否无法使用 进行上述查询 Zend_Db_Select
  • 我需要什么语法才能让它在 Zend Framework 中工作?
4

2 回答 2

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())

我还没有尝试过,但我相信它会起作用。

于 2011-07-12T11:34:59.660 回答
1

... ->joinLeft(array('c' => new Zend_Db_Expr('(' . $subselect->assemble() . ')'), 'c.pkey = a.ukey', array())

于 2011-07-18T16:29:44.627 回答