简体   繁体   English

如何使用CONCAT_WS代替全文搜索

[英]How to use CONCAT_WS instead of FULL-TEXT SEARCH

MySQL 的MySQL

+----+-------------------+----------+--------------+
| id | address           | city     | state        |      
+----+-------------------+----------+--------------+
| 1  | 13000 highway 244 | keystone | south dakota |
+----+-------------------+----------+--------------+

(btw, I left out some columns to make it easier) (顺便说一句,为了简化起见,我省略了一些列)

PHP 的PHP

$string = mysql_real_escape_string('13000 highway 244 south dakota');

$a = mysql_query("

    SELECT * FROM `table` WHERE

    CONCAT_WS(' ', `address`, `city`, `state`) LIKE '%$string%'

");

The above search query doesn't return any results, only the following values would work: 上面的搜索查询不返回任何结果,仅以下values有效:

  • 13000 highway 244 keystone south dakota 13000 Highway 244 Keystone South达科他州
  • 13000 highway 244 keystone 13000高速公路244梯形失真
  • keystone south dakota 基斯通南达科他州

However, I also want to get the following values to work: 但是,我还希望获得以下values才能工作:

  • 13000 highway 244 south dakota 13000 Highway 244南达科他州
  • 13000 highway keystone 13000公路梯形失真
  • south dakota highway keystone 南达科他州公路基石
  • etc. 等等

Is that even possible without relying on full-text search ? 在不依赖full-text search情况下,这是否有可能?

Why not do it code more explicitly like this? 为什么不这样更明确地编写代码?

$address = mysql_real_escape_string('13000 highway 244');
$city = mysql_real_escape_string('keystone');
$state = mysql_real_escape_string('south dakota');

$a = mysql_query( '

    SELECT * 
      FROM `table` 
     WHERE (   ( address = ' . $address . ' )
           AND ( city    = ' . $city    . ' )
           AND ( state   = ' . $state   . ' )
           )
        OR ( state   = ' . $state   . ' )
             .. probably more alternatives ...
');

在传递字符串以进行查询之前,用通配符替换空格,因此即使“ 130 dakoda”也可以工作

$string = str_replace( array('*', '?', ' '), array('%', '_', '%'), $string); //Change norm wildcards to mysql format and replace spaces with wildcards

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

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