2009-06-04 125 views
3

我试图用zf的选择,但没有成功如何在ZF中编写此查询?

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

我试图用ssomething这样写这篇文章,查询:

$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().')'); 

任何人都可以帮我解决这个问题!?

+1

+1,我不明白为什么真正的问题永远不会投了。 – karim79 2009-06-04 17:38:22

回答

1

这应做到:

$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());

输出:

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

不要让我知道如何去,我用于测试下面的代码,但没有测试执行实际查询,因为我没有这样的数据结构:

$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()); 
+0

Yeap它是,但我写了几个实际上是分叉的查询,但你知道它是什么时候,只是它不会去... Tnx帮助反正... – Splendid 2009-06-04 18:33:23

+0

好的回答karim79 - 我upvoted – 2009-06-04 20:37:48

1

你可以试试这个:

$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);