How to write this request in ZF?

I am trying to write this, a query with a choice of zf, but without success

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

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

Anyone can help me solve the problem !?

+3
source share
2 answers

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

Let me know how this happens, I used the code below for testing, but did not test the execution of the actual request, since I do not have 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());
+1
source

You can try the following:

$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);
+1
source

Source: https://habr.com/ru/post/1709791/


All Articles