2009-06-04 122 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);