0

MySQL

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

(顺便说一句,我省略了一些列以使其更容易)

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%'

");

上面的搜索查询不返回任何结果,只有以下内容values有效:

  • 13000 高速公路 244 基石南达科他州
  • 13000高速公路244基石
  • 基石南达科他州

但是,我也想让以下values工作:

  • 13000 高速公路 244 南达科他州
  • 13000公路基石
  • 南达科他州公路基石
  • 等等

这甚至可能不依赖full-text search

4

2 回答 2

1

为什么不像这样更明确地编码呢?

$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 ...
');
于 2012-02-17T23:58:08.743 回答
0

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

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

于 2013-07-31T09:47:14.753 回答