MYSQL查询不等于不应该工作

时间:2013-01-22 09:03:45

标签: mysql

我想做的事情:多字搜索,当用户从下拉列表中选择pub时,它只需要检索表中pub不等于' - '的记录。

出了什么问题:它会进行一些过滤,但最后一条记录是带有' - '的记录

我尝试了什么:

我在MySQL控制台中尝试了以下查询。

我的查询:

SELECT * FROM ticker where pub <> '-' AND (summary LIKE '%funny%' OR cust_exp LIKE '%funny%') OR (summary LIKE '%joke%' OR cust_exp LIKE '%joke%') OR (summary LIKE '%impact%' OR cust_exp LIKE '%impact%') ORDER BY `created` DESC

上述查询是通过以下方式生成的:

构建多字搜索..

$words = explode(" ",$keywords);

        $queryWords = '';
        $i=0;  

        foreach($words as $each){
            $i++;

            if ($i == 1){
                $queryWords.=" (summary LIKE '%$each%' OR cust_exp LIKE '%$each%') ";   
            }
            else{
                $queryWords.=" OR (summary LIKE '%$each%' OR cust_exp LIKE '%$each%') ";    
            }
          }

查询代码片段..

elseif ($ticket_type == 'Pub'){
                if ($status == 'All'){
                        if (empty($product)){
                            $query="SELECT * FROM ticker where pub <> '-' AND".$queryWords."ORDER BY `$order_by` DESC";
                        }

编辑:示例搜索字符串是'有趣的笑话影响'

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT * FROM ticker 
WHERE TRIM(pub) != '-' 
  AND (
    (summary LIKE '%funny%' OR cust_exp LIKE '%funny%') 
    OR
    (summary LIKE '%joke%' OR cust_exp LIKE '%joke%')
    OR 
    (summary LIKE '%impact%' OR cust_exp LIKE '%impact%')
  ) 
ORDER BY `created` DESC

答案 1 :(得分:0)

检查您的查询

  

和 “$ queryWords。” ORDER   你在“AND”和“ORDER BY”之间没有空格

答案 2 :(得分:0)

$ Where [] =“pub&lt;&gt;' - '”;

$ words = explode(“”,$ keywords);

foreach($ words as $ ThisWord)$ WhereWord [] =“summary LIKE'%{$ ThisWord}%'或cast_exp LIKE'%{$ ThisWord}%'”;

$ Where [] =“(”。implode(“OR”,$ WhereWord)。“)”;

$ queryWords =“SELECT * FROM MyTable WHERE”.implode(“AND”,$ Where)。“ORDER BY'{$ order_by}'DESC”;

print $ queryWords;