如何使用CONCAT_WS而不是FULL-TEXT SEARCH

时间:2012-02-17 23:29:57

标签: php mysql concat-ws

的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 highway 244 keystone south dakota
  • 13000高速公路244 keystone
  • keystone south dakota

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

  • 13000 Highway 244 south dakota
  • 13000高速公路keystone
  • 南达科他州高速公路keystone

是否可以在不依赖full-text search

的情况下实现

2 个答案:

答案 0 :(得分: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 ...
');

答案 1 :(得分:0)

在将字符串传递给查询之前

用通配符替换空格,所以即使“130 dakoda”也可以工作

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