views:

178

answers:

2

I am trying to write this, query with zf select but without success

SELECT * FROM `subscribers` WHERE id IN (Select subscriber_id From gs_relations Where group_id=55)

I was trying with ssomething like this:

$gs_relations = new GSRelations();
$part = gs_relations->select()->from('gs_relations',subscriber_id')->where("group_id=$group_id");
$select = $this->select()->setIntegrityCheck(false);
return $select->where('id IN ('.$part->__toString().')');

Anybody can help me to solve the problem!?

+1  A: 

This should do it:

$gs_relations = new GSRelations();
$part = $gs_relations->select()->from('gs_relations','subscriber_id')->where('group_id = ?',$group_id);
$select = $this->select()->setIntegrityCheck(false);
$select->from('subscribers')->where('id in (' . $part->__toString() . ')');
return $select;

print_r($select->__toString());

Output:

SELECT `subscribers`.* FROM `subscribers` WHERE (id in (SELECT `gs_relations`.`subscriber_id` FROM `gs_relations` WHERE (group_id = 55)))

Do let me know how it goes, I used the below code for testing, but did not test executing the actual query as I have no such data structures:

$groupId = 55;
$part = $this->db->select()->from('gs_relations','subscriber_id')->where('group_id = ?',$groupId);
$select = $this->db->select()->from('subscribers')->where('id in (' . $part->__toString() . ')');
print_r($select->__toString());
karim79
Yeap it is, but I wrote few queries that actually was forking, but u know how it is when simply it ain't going...Tnx for help anyway...
Splendid
Good answer karim79 - I upvoted
David Caunt
+1  A: 

You can try this:

$groupId = 55;
$part = $gs_relations->select()->setIntegrityCheck(false)->from('gs_relations','subscriber_id')->where('group_id = ?', $groupId);
$select = $gs_relations->select()->setIntegrityCheck(false)->from('subscribers')->where('id in ?', $part);
Maxim
+1 - I didn't know you could pass a select() to another select()'s placeholder!
karim79