简体   繁体   English

有没有解决方案可以比较SQL全文搜索中的特定字段?

[英]Is there a solution to compare a specific field in a SQL full-text search?

This is my regular code and it works fine: 这是我的常规代码,可以正常工作:

$sql = 'SELECT '
    . '* '
  . 'FROM '
    . 'item '
  . 'WHERE MATCH '
    . '(title) '
  . 'AGAINST '
    . '(:search IN NATURAL LANGUAGE MODE) '
  . 'OR MATCH '
    . '(description) '
  . 'AGAINST '
    . '(:search IN NATURAL LANGUAGE MODE) '
  . 'ORDER BY '
    . 'id ASC';

I want to compare the field "status" to the string "public" because I need only the "public" entrys, I tried something like this: 我想将字段“状态”与字符串“公共”进行比较,因为我只需要“公共”条目,所以我尝试了如下操作:

$sql = 'SELECT '
    . '* '
  . 'FROM '
    . 'item '
  . 'WHERE MATCH '
    . '(title) '
  . 'AGAINST '
    . '(:search IN NATURAL LANGUAGE MODE) '
  . 'OR MATCH '
    . '(description) '
  . 'AGAINST '
    . '(:search IN NATURAL LANGUAGE MODE) '
  . 'WHERE '
    . 'status = "public" '
  . 'ORDER BY '
    . 'id ASC';

But with the last "WHERE" the result is nothing I expected. 但是最后一个“ WHERE”的结果与我预期的一样。

Is there any solution? 有什么解决办法吗?

You have added 2 WHERE statements, thats not allowed and when you use OR it is best to place brackets round the OR condition to ensure the correct result 您添加了2条WHERE语句,那是不允许的,并且当您使用OR时,最好将括号放在OR条件周围,以确保结果正确

$sql = 'SELECT *
        FROM item 
        WHERE status = "public"
        AND (
                MATCH (title) AGAINST (:search IN NATURAL LANGUAGE MODE) 
             OR MATCH (description) AGAINST (:search IN NATURAL LANGUAGE MODE) 
            )
        ORDER BY id ASC';

There are couple of errors in your query, 您的查询中有几个错误,

  1. WHERE clause is added twice WHERE子句被添加两次
  2. Conditions in OR should be enclosed in round brackets '()' OR条件应放​​在方括号'()'中

     SELECT * FROM item WHERE ( MATCH (title) AGAINST (:search IN NATURAL LANGUAGE MODE) OR MATCH (description) AGAINST (:search IN NATURAL LANGUAGE MODE) ) AND status = "public" ORDER BY id ASC 

Also, using SELECT * is harmful. 另外,使用SELECT *是有害的。 You can read the details about it here 您可以在此处阅读有关它的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM