在单个数组中获取两个类别值



我有 2 个类别 'cele_birth' 和 'cele_anniv'。我想在 drupal 7 中的单个查询中获取两个值,我已经尝试了如下。我只返回cele_anniv结果。如何获取两者

$result = db_query('select response from {soap_service} where category = :category',array(':category' => 'cele_birth',':category' => 'cele_anniv'));
foreach($result as $record){
$data = $record->response;
$data = drupal_json_decode($data);
print_r($data);
}

你必须在sql中使用OR条件

$result = db_query('select response from {soap_service} where category = :category1 OR category = :category2',array(':category1' => 'cele_birth',':category2' => 'cele_anniv'));

悉达多的答案是正确的,但当我们有很多值要比较时,就不适合了。还有另一种解决方法,它将减少使用 IN 运算符多次写入 OR 条件。

$result = db_query('select response from {soap_service} where category in (:category)',array(':category' => array('cele_birth', 'cele_anniv')));

有关 IN 运算符的更多详细信息,请参阅此处 SQL in 运算符

最新更新