根据另一个表行比较MySQL表中的值

发布于 2024-12-10 09:25:41 字数 1118 浏览 0 评论 0原文

我正在使用 CakePHP 开发一个应用程序系统。每份申请均包含申请人的地址和邮政编码(仅限英国)。

客户希望能够将“邮政编码区域”分配给其团队的指定成员。 不幸的是,邮政编码区域的格式和长度各不相同 - 示例:TATQ1S60BS22TA6 4EJ

用户希望能够指定整个区域,或者根据需要在每个用户的基础上指定更具体的区域。 schema

我需要能够获取每个前缀并将其与邮政编码进行比较 每个应用程序,并返回任何匹配项。


用户Ross 管理邮政编码以BQ 开头的应用程序。 BSS60

用户 Dave 管理具有完整邮政编码的应用程序:BS22 4JLTA6 7EJ >


我认为最好的方法是有一个充满用户定义的邮政编码前缀的表:

   id  |  prefix
    1  |      TQ
    2  |      TL
    3  |     S50
    4  |     S55
    5  | TA6 4EJ

每个用户都被分配一个邮政编码区域来管理。

我在想,如果我可以将每个应用程序邮政编码与每个前缀进行比较,我就可以检索给定邮政编码区域中的所有应用程序,然后查找该应用程序所属的用户。

id | postcode
1  |  tq33 abc
2  |  TL5  8HA
3  |  s50 11l

不幸的是,邮政编码数据不是任何标准/常规格式;所以这可能会变得棘手。 听起来我需要将前缀加入到邮政编码上;但由于没有钥匙,我认为这是不可能的。

前缀邮政编码之间没有直接关联。感觉我错过了一些基本的东西

有什么指示吗?

I am working on an application system in CakePHP. Each application contains the address and a (UK only) postcode of the applicant.

The client wants to be able to assign "postcode areas" to a given member of their team.
Unfortunately the postcode area varies in format and length - examples: TA, TQ1, S60, BS22, TA6 4EJ

The user wants to be able to specify entire areas, or more specific areas on a per user basis if required.
schema

I need to be able to take each prefix and compare it to the postcode for each application, and return any matches.


User Ross manages applications with postcode beginning with: BQ. BS and S60

User Dave manages applications with full postcodes: BS22 4JL and TA6 7EJ


I figured that the best way would be to have a table full of user-defined postcode prefixes:

   id  |  prefix
    1  |      TQ
    2  |      TL
    3  |     S50
    4  |     S55
    5  | TA6 4EJ

Each user is assigned a postcode area to manage.

I'm thinking if I can compare each application postcode to each prefix, I can retrieve all applications in a given postcode area, then look up the user that this belongs.

id | postcode
1  |  tq33 abc
2  |  TL5  8HA
3  |  s50 11l

The postcode data isn't in any standard/regular format unfortunately; so this might get tricky.
It sounds like I need to JOIN the prefix on to the postcode; but as there's no key I don't think that's possible.

There's no direct association between prefix and postcode. Feel like I'm missing something fundamental

Any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2024-12-17 09:25:42

尝试:

SELECT users.name, app.* from applications app, users, postcodes where app.postcode LIKE CONCAT(postcodes.prefix,'%') AND postcodes.user_id = users.id

try:

SELECT users.name, app.* from applications app, users, postcodes where app.postcode LIKE CONCAT(postcodes.prefix,'%') AND postcodes.user_id = users.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文