如果在Sql搜索中的where和or子句

时间:2016-08-09 03:11:34

标签: mysql if-statement

我使用if,where和or子句构建了一个使用n_make和n_month搜索文本的查询

但是 - 不确定为什么n_month没有被执行,只搜索文本,如果它在n_make中而不在n_month中

我希望搜索显示输出,如果它显示在n_make或n_month

    // Filter by search in title
    $search = $this->getState('filter.search');

    if (!empty($search))
    {
        if (stripos($search, 'id:') === 0)
        {
            $query->where('a.id = ' . (int) substr($search, 3));
        }
        else
        {
            $search = $db->Quote('%' . $db->escape($search, true) . '%');
            $query->where('( a.n_make LIKE ' . $search . ' )')
            or
            $query->where('( a.n_month LIKE ' . $search . ' )')
            ;
        }       
    }

1 个答案:

答案 0 :(得分:1)

    else
    {
        $search = $db->Quote('%' . $db->escape($search, true) . '%');
        $query->where('( a.n_make LIKE' . $search . ') OR ( a.n_month LIKE' . $search . ')');
    }