2008年11月21日星期五

Re: [fw-db] Nested SELECT statements in $select->from

I've done this before by using two separate select objects.

$subSelect = $db->select()->from(
    array(
        't1' => 'table1'
    )
)->where('t1.catID = ?', 5);

Then your outer select can use $subSelect by casting it to a string and wrapping that it in paranthesis inside a Zend_Db_Expr object:

$select = $db->select()->from(
    array(
        'tmp' => new Zend_Db_Expr('(' . (string) $subSelect . ')') //
    )
)->where('tmp.body LIKE ?', '%' . $searchword . '%');

echo $select; // should look correct

-Hector


On Fri, Nov 21, 2008 at 8:03 AM, Chris Tankersley <chris@tankersleywebsolutions.com> wrote:
Is there a way to do the following SQL:

SELECT * FROM (SELECT * FROM `table1` AS `t1` WHERE t1.catID = 5) AS
`tmp` WHERE tmp.body LIKE '%searchword%'

(specifically the nested SELECT in the FROM) using the object
interface of the Zend_Db_Select object? I've got a SQL query that is
more complicated than the above but in straight SQL is made much
simpler using nested SELECT. I'd like to use Zend_Db_Select as it
makes building SQL statements so much nicer when generating SQL with
code.

Chris


没有评论: