[英]lithium framework how to make a query with OR
我开始使用锂 PHP 框架。 我必须进行查询以获取在“title”字段中包含“%test%”或“%easy%”的问题。 我尝试使用以下代码来做到这一点:
$questions = Questions::find('all', array(
'conditions' => array(
'name' => array(
'like' => array(
'%easy%',
'%test%'
)
)
)
));
但它使这个查询:
SELECT * FROM `questions` AS `Questions` WHERE (`name` LIKE '%easy%' AND `name` LIKE '%test%')
如何用OR替换AND?
作为您问题的解决方案,您可以在 (=) 中使用,而不是像
'conditions' => array(
'name' => array(
'=' => array(
'%easy%',
'%test%'
)
)
)
这将生成查询:
WHERE ((`name` IN ('%easy%', '%test%')))
或者可以在使用两个不同字段进行搜索时使用:
'conditions' => array(
'OR' => array(
'name' => array(
'=' => array(
'%easy%',
'%test%'
)
),
'name2' => array(
'!=' => array(
'%easy%',
'%test%'
)
)
)
),
这将生成查询:
WHERE ((`name` IN ('%easy%', '%test%')) OR (`name2` IN ('%easy%', '%test%')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.