简体   繁体   English

搜索MySQL数据库,其中字段包含符号

[英]Search MySQL database where fields contain symbols

I have a location database on my MySQL server containing UK postcodes, postal towns, counties and coordinates. 我的MySQL服务器上有一个位置数据库,其中包含英国邮政编码,邮政城镇,县和坐标。 If one were to search for "connahs" (real result is "Connah's Quay"), no results would be returned because they did not include the apostrophe. 如果要搜索“ connahs”(实际结果是“ Connah's Quay”),则不会返回任何结果,因为它们不包含撇号。 The same applies to hyphens and other symbols. 连字符和其他符号也是如此。

In this example, the postal town "Connah's Quay" is the correct format, so I can't just remove the symbol from the database. 在此示例中,邮政小镇“ Connah's Quay”是正确的格式,因此我不能仅从数据库中删除该符号。

My query so far is: 到目前为止,我的查询是:

SELECT * FROM uk_postcodes WHERE postal_town LIKE "connahs%" ORDER BY postal_town LIMIT 25

Is there any way I can search for fields both containing symbols (connah's) and not (connahs) so that regardless of whether the user uses the apostrophe it returns results? 有什么方法可以搜索包含符号(connah的)和不包含符号(connahs)的字段,以便无论用户是否使用撇号,它都将返回结果?

Use mysql replace function. 使用mysql替换功能。 Query will be like this: 查询将是这样的:

SELECT * FROM uk_postcodes WHERE  REPLACE( postal_town, "'", "" )   
LIKE "connahs%" ORDER BY postal_town LIMIT 25

Hope it will help you. 希望对您有帮助。

I assume you are using php in your code. 我假设您在代码中使用php。

// get the value of searched word from searchbox or something...
$keyword = $_POST['uk_postcodes'];
// avoid sql injection to, also read for special characters
$keyword = mysql_real_escape_string($keyword);
$keyword = stripslashes($keyword);

$query = "SELECT * FROM uk_postcodes WHERE postal_town LIKE %$keyword ORDER BY postal_town LIMIT 25";

In the code above, you can type anything! 在上面的代码中,您可以输入任何内容!

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

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