【问题标题】:Where In Not - doctrineWhere In Not - 学说
【发布时间】:2012-02-03 21:41:40
【问题描述】:
FIRST:
id | name
1  | sss
2  | ddd
5  | fff

  $q = Doctrine_Query::create()
    ->from('First a');

  return $q->execute();

SECOND:
id | first_id | name
 ....


  $two = Doctrine_Query::create()
    ->from('Second a')
    ->whereInNot(first_id, $q) // ???????
    ;

  return $two->execute();

如果表 FIRST 中不存在 first_id,如何从表 SECOND 中获取所有数据?

【问题讨论】:

  • 您的代码的问题包括:第一个查询返回一个 Doctrine_Collection(如 Doctrine_Records 列表),它不是您可以输入第二个查询的数组。此外,您需要的方法是“whereNotIn()”而不是“whereInNot()”。我在回答中提供了两个示例,说明如何编写代码,具体取决于您是否真的需要知道 first_ids。

标签: php sql symfony1 doctrine symfony-1.4


【解决方案1】:

方法一(类似于您的示例代码):

// Retrieve ONLY the id, and use hydration mode that produces a simple array of results.
$first_ids = Doctrine_Query::create()
  ->select('f.id')
  ->from('First f')
  ->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);

$two = Doctrine_Query::create()
  ->from('Second s')
  ->whereNotIn('first_id', $first_ids);
return $two->execute();

在单个复合查询中获得相同结果的另一种方法:

$two = Doctrine_Query::create()
  ->from('Second s')
  ->where('first_id NOT IN (SELECT id FROM First)');
return $two->execute();

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    相关资源
    最近更新 更多