【问题标题】:cakephp complex query multiple 'OR' conditioncakephp复杂查询多个'OR'条件
【发布时间】:2012-12-03 03:56:38
【问题描述】:

我想用 cakephp 进行这样的查询:

WHERE text LIKE '%keyword%' 
AND 
(
    (text LIKE '%something%') 
    OR (text LIKE '%something%') 
    OR (...)
) 
AND 
(
    (text LIKE '%other%') 
    OR (text LIKE '%other%') 
    OR (...)
) 
NOT 
(
    (text LIKE '%dont include%') 
    OR (text LIKE '%dont include%') 
    OR (...)
)

这是我的 $conditions 代码:

$conditions = array
(
    'Tweet.text LIKE' => '%keyword%',
    'AND' => array(
        array(
            'OR' => array(
                // topic
                array('Tweet.text LIKE' => '%something%'),
                array('Tweet.text LIKE' => '%something%')
            )
        ),
        array(
            'OR' => array(
                // sentiment
                array('Tweet.text LIKE' => '%other%'),
                array('Tweet.text LIKE' => '%other%')
            )
        )
    ),
    'NOT' => array(
        array('Tweet.text LIKE' => '%dont include%'),
        array('Tweet.text LIKE' => '%dont include%')
    )
);

我正在使用 Debugger::dump() 方法显示结果,结果只是使用了最后一个“OR”条件,而不是两个“OR”条件:

array(
    'Tweet.text LIKE' => '%keyword%',
    'OR' => array(
        (int) 0 => array(
            'Tweet.text LIKE' => '%other%'
        ),
        (int) 1 => array(
            'Tweet.text LIKE' => '%other%'
        )
    ),
    'NOT' => array(
        (int) 0 => array(
            'Tweet.text LIKE' => '%dont include%'
        ),
        (int) 1 => array(
            'Tweet.text LIKE' => '%dont include%'
        )
    )
)

我的问题是,如何进行同时使用“OR”条件的查询?

请尽快回复..提前谢谢:)

【问题讨论】:

  • 好......问题朋友。

标签: sql cakephp


【解决方案1】:

尝试以下方法:

$conditions = array(
    'Tweet.text LIKE' => '%aa%',  //implied and
    array( //implied and
        'or' => array(
            array('Tweet.text LIKE' => '%11%'),
            array('Tweet.text LIKE' => '%22%'),
            array('Tweet.text LIKE' => '%33%'),
            ...
        )   
    ),
    array( //implied and
        'or' => array(
            array('Tweet.text LIKE' => '%123%'),
            array('Tweet.text LIKE' => '%456%'),
            array('Tweet.text LIKE' => '%789%'),
            ...
        )   
    )
    'not' => array(
        'or' => array(
            array('Tweet.text LIKE' => '%x%'),
            array('Tweet.text LIKE' => '%y%'),
            array('Tweet.text LIKE' => '%z%'),
            ...
        )   
    )
)

text LIKE aa 
    AND ( either 11, 22, 33 ) 
    AND (either 123, 456, 789) 
    BUT NOT (x || y || z)`

任何未指定orandnot 的数组都是and。无需手动指定。

【讨论】:

  • 关于如何在 Cake3 中使用 where()、andWhere() 和 orWhere() 做同样事情的任何想法
猜你喜欢
  • 2018-10-18
  • 2012-08-27
  • 2021-08-07
  • 2014-03-22
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
相关资源
最近更新 更多