gpt4 book ai didi

sql - 如何用另一个 sql 查询编写WhereIn()

转载 作者:行者123 更新时间:2023-12-02 08:52:54 25 4
gpt4 key购买 nike

我有以下 SQL 查询

... where group_id IN (select group_id from alert where monitor_id = 4);

我想用 Doctrine 写它,但我不知道如何将 IN select 添加到 WHEREIN() 子句中!有什么想法吗?

这就是我所做的

$q = $this->createQuery('u') 
->select('u.email_address')
->distinct(true)
// ->from('sf_guard_user u')
->innerJoin('u.sfGuardUserGroup ug')
->where('ug.group_id IN(select group_id from alert where monitor_id=?',$monitor);

$q->execute();

在 sfGuardUserTable.class 中:

public function getMailsByMonitor($monitor) {


$q = Doctrine_Query::create()->from("alert a")->where("a.monitor_id", $monitor);
$groups_raw = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
$groups = array();
print_r($groups_raw);
foreach ($groups_raw as $gr) {
$groups[] = $gr->id; //line 33
}


$q2 = $this->createQuery('u')
->select('u.email_address')
->distinct(true)
->innerJoin('u.sfGuardUserGroup ug')
->whereIn("ug.group_id", $groups);
return $q2->execute();
}

最佳答案

通常你会这样做:

$q = Doctrine_Query::create()
->from('User u')
->whereIn('u.id', array(1, 2, 3));

但我认为这个更适合您的需求:

$q = Doctrine_Query::create()
->from('Foo f')
->where('f.group_id IN (SELECT f.group_id FROM Alert a WHERE a.monitor_id = ?)', 4);

关于sql - 如何用另一个 sql 查询编写WhereIn(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7203086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com