doushichi3678 2013-03-19 23:58
浏览 24

MySQL返回结果类似于字符串输入但没有句点

I have something in the database under the region column named "St. Catharines"

I would like to return the records containing "St. Catharines" from a string without the period. So, the string would be "St Catharines". Can this be done? How?

Right now I have:

$region = "St Catharines"; 
$query = " AND region like '" . $region . "%'";

Unfortunately, it does not work.

The period would always be absent from the $region string and could be anywhere within the database record (i.e. not just after "St").

  • 写回答

2条回答 默认 最新

  • douxun3496 2013-03-20 00:07
    关注

    You really shouldn't do that, it's not good, that should be fixed before hand.

    You can split the string by spaces (using explode), and search for example the two parts:
    Like a: $arr = explode(" ",$string);

    $query = " AND (region LIKE '".$arr[0]."%' OR '%".$arr[1]."%')";

    This would find as a result everything with "St" in the beginning and everything that contained "Catharines" in the result.

    That is the best solution to that problem, is partial matching, while it might show more results then the ones you want, another solution is something like mentioned here: stackoverflow question about removing commas and etc

    评论

报告相同问题?