0

我正在尝试做的事情:多词搜索,当用户从下拉列表中选择 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";
                        }

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

4

3 回答 3

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
于 2013-01-22T09:08:26.340 回答
0

检查您的查询

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

于 2013-01-22T09:06:25.080 回答
0

$Where[] = "pub <> '-'";

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

foreach($words as $ThisWord)$WhereWord[] = "summary LIKE '%{$ThisWord}%' OR cast_exp LIKE '%{$ThisWord}%'";

$Where[] = "(".implode(" OR ", $WhereWord).")";

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

打印 $queryWords;

于 2013-01-22T09:15:19.467 回答