0

I have a Store table containing a deliveryPostcodes field, which is a comma-separated list of postcode areas:

id    deliveryPostcodes
1     EC1,EC2,N1
2     EC1,WC1,WC2
3     N1,N2

I'd like to query this table for stores matching a postcode, for example if the user enters EC2A 9XY, it should return the first row, as this one has an entry in the list that is a prefix of the full postcode.

Is there a solution to do that without a custom function?

Note: I know I could do this by joining a separate table with one postcode area per line, but I'm exploring the different options for now.

4

2 回答 2

0

不是真正的答案,而是这个问题的唯一正确解决方案:规范化数据并将其放在单独的表中。由于您的解决方案是:

  1. 不利于索引
  2. 不利于搜索
  3. 用正则表达式解决这个问题将对性能产生巨大影响
  4. 一旦您的数据库开始增长,这种结构将导致问题。

带有邮政编码的查找表也可能在以后派上用场。您可以在 google 上轻松找到您想要的国家/地区(或 contries)的邮政编码列表,甚至可能采用您可以通过最少的工作导入 MySQL 的格式。

于 2013-09-30T14:42:37.073 回答
0

假设 DeliveryPostcodes 是数值,您可以尝试像这样使用正则表达式匹配。

SELECT * 
FROM Store
WHERE deliveryPostcodes RLIKE '.*,?[0-9]+,?.*'
于 2013-09-30T10:14:21.330 回答