DRUPAL db_or在多个变量之间添加AND

时间:2014-08-21 18:35:49

标签: sql drupal drupal-7

我继承了一个使用drupal过滤系统来查找结果的应用程序。用户可以选择搜索“campus_location”'列,但是当它们输入多个变量时,它会在条件之间添加一个AND。

以下是drupal生成的查询的打印版本:

SELECT * FROM champs c INNER JOIN users u ON u.uid = c.uid LEFT OUTER JOIN champs_rsvp r 
ON r.uid = c.uid LEFT OUTER JOIN champs_checklists cl 
ON cl.id = c.checklist_id LEFT OUTER JOIN node en 
ON en.uid = u.uid AND en.type = 'getting_started_event' 
LEFT OUTER JOIN node sn ON sn.uid = u.uid 
AND sn.type = 'success_story' 
LEFT OUTER JOIN champs_action_steps cas 
ON cas.uid = u.uid 
WHERE ( (c.campus_location LIKE 'Flint' ESCAPE '\\') )
**AND**( (c.campus_location LIKE 'Ann Arbor' ESCAPE '\\') ) 

我需要最后一个AND成为OR,所以它看起来像这样:

SELECT * FROM champs c INNER JOIN users u ON u.uid = c.uid LEFT OUTER JOIN champs_rsvp r 
ON r.uid = c.uid LEFT OUTER JOIN champs_checklists cl 
ON cl.id = c.checklist_id LEFT OUTER JOIN node en 
ON en.uid = u.uid AND en.type = 'getting_started_event' 
LEFT OUTER JOIN node sn ON sn.uid = u.uid 
AND sn.type = 'success_story' 
LEFT OUTER JOIN champs_action_steps cas 
ON cas.uid = u.uid 
WHERE ( (c.campus_location LIKE 'Flint' ESCAPE '\\') )
OR( (c.campus_location LIKE 'Ann Arbor' ESCAPE '\\') ) 

产生正确的结果。

以下是我在搜索框中查看单词时继承的代码。我似乎无法弄清楚它在哪里添加变量将它放在AND。

$words = explode(",", $_SESSION['form_state']['values']['filter']);

    foreach($words as $word)
    {

        $or = db_or();
        foreach($cols as $col)
        {
            $table = 'c';
            if(in_array($col, array('completed_date', 'score')))
                $table = 'cl';
            elseif(in_array($col, array('name', 'mail')))
                $table = 'u';
            elseif($col == 'date')
                $table = 'r';
            elseif(in_array($col, array('announce', 'num_success_stories', 'num_action_steps')))    //can't put these in WHERE clause. Need to go in HAVING. 
                continue;
            echo $word;
            $or = $or->condition($table . '.' . $col, champs_make_like_sql($word), 'LIKE');

        }
        $result = $query->condition($or);
    }

0 个答案:

没有答案