2009年3月18日星期三

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

Thanks for that.

I have been toying with the idea of a second select done that way. I just
didn't know if there was a cleaner way or not.

The reason for the parent::fetchAll was that this class (for testing) does
have a slightly modified fetchAll(). The example I copied is only a snippet
of the whole class and when I was writing this method I wanted to use the
proper fetchAll and not my version.

One other thing, the SQL I showed is using an ARTICLES table. The PHP code
uses a _productstest table. I'm mentioning this just in case anyone can't
follow the example correctly.

Brenton Alker-3 wrote:
>
> 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/
>
>
>
>

--
View this message in context: http://www.nabble.com/SELECT-WHERE-IN-%28SELECT%29-question-tp22566627p22577201.html
Sent from the Zend DB mailing list archive at Nabble.com.

没有评论: