0

这是我想通过 Yii 实现的 SQL CDBCriteria

WHERE 
    (
        (field1 LIKE '%value1%') AND (field1 LIKE '%value2%')
    ) OR (
        (field2 LIKE '%value1%') AND (field2 LIKE '%value2%')
    ) OR (
        (field3 LIKE '%value1%') AND (field3 LIKE '%value2%')
    )

如果字符串以任何顺序包含“value1”或“value2”,这将返回该行。前任。:

Lorem value1 ipsum value2 先生 dolor amet。

或者

Lorem value2 ipsum value1 先生 dolor amet。

但如果只有一个值,则不会返回任何内容。

我尝试过的(来自这个线程):

$c = new CDbCriteria;
$c->addColumnCondition(array('field1 LIKE' => '%value1%', 'field1 LIKE' => '%value2%'), 'AND', 'OR');
$c->addColumnCondition(array('field2 LIKE' => '%value1%', 'field2 LIKE' => '%value2%'), 'AND', 'OR');
$c->addColumnCondition(array('field3 LIKE' => '%value1%', 'field3 LIKE' => '%value2%'), 'AND', 'OR');

但这不是如何在LIKE条件下使用它,它只返回第二个比较,因为同一字段的键是相同的:

(
    (field1 LIKE='%value2%') OR (field2 LIKE='%value2%')
) OR (field3 LIKE='%value2%')

所以我尝试了比较条件:

$c->compare('field1', 'value1', true);
$c->compare('field1', 'value2', true, 'OR');
$c->compare('field2', 'value1', true, 'AND');
$c->compare('field2', 'value2', true, 'OR');
$c->compare('field3', 'value1', true, 'AND');
$c->compare('field3', 'value2', true, 'OR');

或者

$c->addSearchCondition('field1', 'value1', false);
$c->addSearchCondition('field1', 'value2', false, 'OR');
$c->addSearchCondition('field2', 'value1', false);
$c->addSearchCondition('field2', 'value2', false, 'OR');
$c->addSearchCondition('field3', 'value1', false);
$c->addSearchCondition('field3', 'value2', false, 'OR');

但我无法解决我想要的条件。这是我得到的:

(
    (
        (
            (
                (field1 LIKE '%value1%') OR (field1 LIKE '%value2%')
            ) 
            AND (field2 LIKE '%value1%')
        ) 
        OR (field2 LIKE '%value2%')
    ) 
    AND (field3 LIKE '%value1%')
) OR (field3 LIKE '%value2%')

因此,如果有人知道如何实现这一目标,请帮助我:)

4

1 回答 1

6

这是你想要的?

$c->addCondition("field1 like :value1 and field1 like :value2", "OR");
$c->addCondition("field2 like :value1 and field2 like :value2", "OR");
$c->addCondition("field3 like :value1 and field3 like :value2", "OR");
$c->params = array(':value1' => '%'. $value1 .'%', ':value2' => '%'. $value2 .'%');

选择:

$c2 = new CDbCriteria;
$c2->addSearchCondition("field2", ':value1');
$c2->addSearchCondition("field2", ':value2');

$c3 = new CDbCriteria;
$c3->addSearchCondition("field3", ':value1');
$c3->addSearchCondition("field3", ':value2');

$c->addSearchCondition("field1", ':value1');
$c->addSearchCondition("field1", ':value2');
$c->mergeWith($c2, false);
$c->mergeWith($c3, false);
于 2013-02-11T16:02:42.653 回答