[英]lithium framework how to make a query with OR
I start work with lithium PHP framework.我开始使用锂 PHP 框架。 I have to make a query to get questions which have '%test%' or '%easy%' in 'title' field.
我必须进行查询以获取在“title”字段中包含“%test%”或“%easy%”的问题。 I've tried to do it using following code:
我尝试使用以下代码来做到这一点:
$questions = Questions::find('all', array(
'conditions' => array(
'name' => array(
'like' => array(
'%easy%',
'%test%'
)
)
)
));
but it makes this query:但它使这个查询:
SELECT * FROM `questions` AS `Questions` WHERE (`name` LIKE '%easy%' AND `name` LIKE '%test%')
how to replace AND with OR?如何用OR替换AND?
as a solution to your question you can use in (=) instead of like作为您问题的解决方案,您可以在 (=) 中使用,而不是像
'conditions' => array(
'name' => array(
'=' => array(
'%easy%',
'%test%'
)
)
)
this will generate the query:这将生成查询:
WHERE ((`name` IN ('%easy%', '%test%')))
Or can be used when searching using two different fields:或者可以在使用两个不同字段进行搜索时使用:
'conditions' => array(
'OR' => array(
'name' => array(
'=' => array(
'%easy%',
'%test%'
)
),
'name2' => array(
'!=' => array(
'%easy%',
'%test%'
)
)
)
),
And this will generate query:这将生成查询:
WHERE ((`name` IN ('%easy%', '%test%')) OR (`name2` IN ('%easy%', '%test%')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.