【问题标题】:doctrine how to write WhereIn() with another sql query inside教义如何编写 WhereIn() 里面有另一个 sql 查询
【发布时间】:2011-11-04 09:49:06
【问题描述】:

我在 SQL 中有以下查询

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

我想用 Doctrine 编写它,但我不知道如何将 IN 选择添加到 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();
    }

【问题讨论】:

  • 与实际问题无关,但与此查询的使用有关:您使用的是哪个 RDBMS?
  • 你是使用 Symfony-1.4 和 Doctrine 1.2 还是使用 Symfony2 和 Doctrine2? (混淆标签)
  • Symfony-1.4 和 Doctrine 1.2
  • // before ->from('sf_guard_user u')` 相关吗?
  • @SymFoNyBegginer:考虑使用JOIN 重写查询,甚至使用WHERE EXISTS 比使用IN 更好。众所周知,MySQL 对x IN (SELECT y FROM z) 的执行计划不是最优的。如果您的表很小(记录数),这还不错,但如果它们很大,您会注意到速度上的差异。

标签: sql symfony1 doctrine symfony-1.4 doctrine-1.2


【解决方案1】:

一种可能的解决方案 - 将select group_id from alert where monitor_id = 4 提取到一个数组中并在whereIn 中使用它

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

$q2 = Doctrine_Query::create()->from("table t")->whereIn("t.group_id", $groups);

两个查询而不是一个,但绝对可以解决问题。

【讨论】:

  • 我不太明白你的问题!
  • 思路:在第一个查询中将所有组 id 提取到一个数组中,然后在第二个查询中使用whereIn 中的这个数组来检索最终结果。
  • 我得到这个错误:c:\Dev\sfMonitoring>symfony monitorRetreive PHP 注意:试图在 C:\Dev\sfMonitoring\lib\mod el\doctrine\sfDoctrineGuardPlugin\ 中获取非对象的属性sfGuardUserTable.class.php 第 33 行
  • 这是第 33 行:$groups[] = $gr->id;
  • 这对我来说毫无意义。我这里没有你的代码,你能用产生这个错误的代码更新问题吗(请不要忘记标记第 33 行 :)
【解决方案2】:

通常你会做这样的事情:

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

【讨论】:

  • 为什么要从alert中选择a.group_id(确保中间没有空格)。
  • SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册 f 或在第 1 行的 '' 附近使用的正确语法
  • 是否可以将相关的 schema.yml 部分添加到您的问题和当前的 dql 查询中?
  • 这就是查询的平静:->where('ug.group_id IN(select group_id from alert where monitor_id=?',$monitor);
  • $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();
猜你喜欢
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多