推进 "AND NOT" 查询

Propel "AND NOT" query

我在 PHP 应用程序中使用 propel ORM。

通过阅读文档,我不知道如何发出这种类型的请求:

SELECT x FROM table where col1 = 'xxx' and not(col2=1 AND col3=2);

使用纯推进逻辑执行此请求的最简洁方法是什么?

谢谢

您的查询相当于:

SELECT x FROM table where col1 = 'xxx' and (col2 != 1 OR col3 != 2);

假设你使用的是 propel 2,你应该能够完成你想要的:

$result = TableQuery::create()
    ->filterByCol1('xxx')
    ->filterByCol2(1, Criteria:NOT_EQUAL)
    ->_or()
    ->filterByCol3(2, Criteria:NOT_EQUAL)
    ->find();

您要查找的完整查询应该是这个:

$books = TableQuery::create()
    ->condition('cond1', 'col1 = ?', 'xxx')
    ->condition('cond2', 'col2 != ?', 1)
    ->condition('cond3', 'col3 != ?', 2)
    ->combine(array('cond2', 'cond3'), 'or', 'cond23')
    ->where(array('cond1', 'cond23'), 'and')
    ->find();

它创建:

  • a cond1 条件 where col1 = 'xxx'
  • a cond2 条件 col2 != 1
  • a cond3 条件 col3 != 2

然后它将 cond2cond3or 运算符组合在一个新的 cond23,所以

cond23 = cond2 or cond3

并将 cond23cond1and 运算符组合,以便最终查询将会

where(cond1 and cond23)