MySQL
表:城市
+----+----------+
| id | city |
+----+----------+
| 1 | New York |
+----+----------+表:台站
+----+--------------+---------+
| id | name | city_id |
+----+--------------+---------+
| 1 | Woodlawn | 1 |
+----+--------------+---------+
| 2 | Mosholu Pkwy | 1 |
+----+--------------+---------+PHP
$string = mysql_real_escape_string('woodlawn new york');
$a = mysql_query("
SELECT s.id FROM cities AS c, stations AS s
WHERE CONCAT_WS(' ', s.name, c.city) LIKE '%$string%' AND c.id = s.city_id
");
while($b = mysql_fetch_assoc($a))
{
echo $b['id'];
}问题
但是,当我试图搜索以下内容时,上面的示例工作得很好:
$string = mysql_real_escape_string('mosholu new york');我得到0的结果,除非我显式地搜索mosholu纽约。
可能的选择?
显然,我不能将FULLTEXT搜索与CONCAT_WS结合使用。我也很想保持这些表的结构,有一个用于cities的表,一个用于stations的表。
因此,向stations表中添加第四列(包括站点名和城市名称),然后对其执行FULLTEXT搜索,这是唯一的选择吗?
但是,如果我必须更新城市名称,然后将其更改为stations表中的所有行,那就太麻烦了。
发布于 2012-05-29 13:54:39
只需将$string中的空白替换为%通配符--您可以根据自己的喜好在PHP或MySQL中这样做;例如,在MySQL中:
SELECT s.id
FROM cities AS c JOIN stations AS s ON c.id = s.city_id
WHERE CONCAT_WS(' ', s.name, c.city) LIKE REPLACE('%$string%', ' ', '%')https://stackoverflow.com/questions/10800420
复制相似问题