简体   繁体   English

mysql查询中合适的where子句

[英]Suitable where clause in mysql query

Background 背景

A table with the following columns and data such as: 具有以下列和数据的表,例如:

---------------------------------------------------------------------------------
| Address | AddressLine2 | AddressLine3 | Locality | District | State | Country |
---------------------------------------------------------------------------------
|Sector-12|              |              |          | Faridabad| Har   | IN      |
| 311     |              |              |  Sector 3| Faridabad| Har   | IN      |
|Sector-1,|              |              |          | Faridabad| Har   | IN      |
|Plot 31  |              |              | Old Fari | Faridabad| Har   | IN      |
|         |              |              |dabad     |          |       |         |
---------------------------------------------------------------------------------

What I am trying to depict here is that the data in Address, AddressLine2, AddressLine 3, Locality can be absolutely random. 我要在这里描述的是Address,AddressLine2,AddressLine 3,Locality中的数据可以绝对随机。

Requirement 需求

Detect current location and find nearby schools in the database with above columns in the school table. 检测当前位置,并在学校表上方的列中找到数据库中附近的学校。

Process 处理

Nearby schools are located using the Google API which returns the names of the schools along with their address. 使用Google API查找附近的学校,该API返回学校的名称及其地址。

Eg returned data: 例如,返回的数据:

Name: St. Albans School
Address: Sector 1, Old Faridabad, Faridabad, Har, IN

Expected result: School with Address Sector-1 and Locality as Old Faridabad 预期结果:地址Sector-1且地点为Old Faridabad学校


District, State and Country are automatically taken using the current location. 使用当前位置自动获取地区,州和国家/地区。

In order to process the rest of the address what is currently taking place is the school address exploded based on , and a mysql query is produced which uses IN . 为了处理剩余的地址,当前发生的是基于学校地址的分解,并产生一个使用IN的mysql查询。

Example query: 查询示例:

 Select * from Schools where Address IN ('Sector-1', 'Sector 1', Sector- 1'. 'Old Faridabad', ...) 

However a problem with IN query is: - If the address column contains a string such as "Sector-1," , the match with fail. 但是, IN query问题是:-如果地址列包含诸如"Sector-1,"类的字符串,则匹配失败。

Another approach: 另一种方法:

 Select * from Schools where Address REGEX "Sector-1|Sector 1|Sector- 1|Old Faridabad|..." 

Problem with this approach, if a school address is like: 314, Sector 19, Faridabad. 如果学校地址是这样的话,这种方法就会出现问题:法里达巴德19区314号。 Splitting the string based on , results in a search criteria of 314 in the query. 基于拆分字符串,查询中的搜索条件为314 Hence a school with address: 314, Sector 31, Faridabad will also result in a match though these schools are far away from each other. 因此,地址为:法里达巴德31区314的学校也将进行比赛,尽管这些学校彼此距离较远。

Any suggestions on how to write a mysql query which helps in selecting the correct schools. 有关如何编写mysql查询的任何建议,有助于选择正确的学校。

NOTE: Address can also contain text like: "314, Sector 19" together. 注意:地址也可以包含类似"314, Sector 19"文本。

听起来您只需要检查开头和结尾的逗号即可:

REGEX "(^|, )(Sector-1|Sector 1|Sector- 1|Old Faridabad|...)(,|$)"

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

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