如何在ZF中编写此查询?

时间:2009-06-04 17:07:21

标签: php sql zend-framework

我正在尝试写这个,用zf select查询但没有成功

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

我正在尝试像这样的东西:

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

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

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分: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);