这是我想通过 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%')
因此,如果有人知道如何实现这一目标,请帮助我:)