2009年3月18日星期三

Re: [fw-db] SELECT WHERE IN (SELECT) question

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAknAqTIACgkQ7bkAtAithusyhACfYFfSAzpUldegLMFM+rwaP5Gc
WqUAoKdt3hRi7eVNz8ahlViYei/Ynu9M
=8ZfZ
-----END PGP SIGNATURE-----
Hi,


SirEdward wrote:

...
> So far I've come up with this:
...
> ->where('node.category_id IN (SELECT DISTINCT category_id FROM
> _productstest WHERE published = 1)')
...
> It works, but is there any way I can improve on the final WHERE clause with
> the IN predicate?
>
> Thanks!

You can use a new select as a value. So you can do:

$subSelect = $this->select()->from('_productstest',
array('category_id'))->where('published = 1');

Then put it into your main select:

->where('node.category_id IN ?', $subSelect)


On a side note, I'm not sure why you're using parent::select(). unless
you've overridden the select method, $this->select should be fine.

--

Brenton Alker
PHP Developer - Brisbane, Australia

http://blog.tekerson.com/

没有评论: