2008年9月26日星期五

Re: [fw-db] writing SELECT query

Anees wrote:
>
> i need a small help
> i couldn't use IN clause in my WHERE condition using $db->select()
>

Two possible solutions. The first builds the subquery as a separate Select
object, and as it interpolates that object into the string of the outer
query, it renders it as a SQL string:

$subselect = $db->select()
->from('table2', 'table1_id')
->where('table2_id IN ('.join(',', array(4, 2, 1, 3)).')');

$select = $db->select()
->from('table1')
->where("id IN ($subselect)");

But in this example, you can also use a join:

$select = $db->select()->distinct()
->from('table1')
->join('table2', 'table1.id = table2.table1_id', array())
->where('table2_id IN ('.join(',', array(4, 2, 1, 3)).')');

Regards,
Bill Karwin
--
View this message in context: http://www.nabble.com/writing-SELECT-query-tp19684996p19692228.html
Sent from the Zend DB mailing list archive at Nabble.com.

没有评论: