本文介绍了SQL查询以匹配多个字符串之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表中有以下数据:
+ --------------- ------- + ------------------------------------------ ---------------- + -------------- +
| subscription_fields_id |名称| field_type |
+ ---------------------- + ---------------------- ------------------------------------ + ------------- -+
| 143 |白沙瓦/伊斯兰堡/拉合尔/特警/马尔丹/卡拉奇|工作地点
| 146 |卡拉奇|工作地点
| 147 |拉合尔和卡拉奇|工作地点
| 149 |卡拉奇,米尔布尔·卡斯,苏库尔,拉耶,吉尔吉特,查萨达|工作地点
| 152 |伊斯兰堡或拉合尔|工作地点
| 155 |伊斯兰堡|工作地点
| 157 |信德省和卡拉奇7区|工作地点
+ ---------------------- + ---------------------- ------------------------------------ + ------------- -+
我的查询是:
select *从用户区域
中选择,其中的名称如'%Khairpur,Islamabad,Karachi%';
结果:
+ ---------------------- + --------------------- -------------------------- + -------------- +
| subscription_fields_id |名称| field_type |
+ ---------------------- + ---------------------- ------------------------- + -------------- +
| 143 |白沙瓦/伊斯兰堡/拉合尔/特警/马尔丹/卡拉奇|工作地点
| 152 |伊斯兰堡或拉合尔|工作地点
| 155 |伊斯兰堡|工作地点
+ ---------------------- + ---------------------- ------------------------- + -------------- +
它应该返回名称包括伊斯兰堡,海毛尔或卡拉奇但未包含在内的所有行。
解决方案
要获得适当的解决方案,请,或者除此以外,考虑。 p>
要快速解决当前的问题,请使用或三个简单的表达式:
选择*
FROM Subscriberfields
WHERE名称〜’((Khairpur |伊斯兰堡| Karachi)’);
或:
...
WHERE(名称为'%Khairpur%'或
名称为'%Islamabad%'或
名称为'%Karachi%')
或使用〜*
或 ILIKE
用于不区分大小写的匹配。
由于另一个答案建议:从不使用 SIMILAR到
:
I have following data in table:
+----------------------+----------------------------------------------------------+--------------+
| subscriber_fields_id | name | field_type |
+----------------------+----------------------------------------------------------+--------------+
| 143 | Peshawar/Islamabad/Lahore/Swat/Mardan/Karachi | Job Location |
| 146 | Karachi | Job Location |
| 147 | Lahore and Karachi | Job Location |
| 149 | Karachi, Mirpur Khas, Sukkur, Layyah, Gilgit, Charsaddah | Job Location |
| 152 | Islamabad or Lahore | Job Location |
| 155 | Islamabad | Job Location |
| 157 | 7 Districts of Sindh and Karachi | Job Location |
+----------------------+----------------------------------------------------------+--------------+
My query is:
select * from subscriberfields
where name like '%Khairpur,Islamabad,Karachi%';
Result:
+----------------------+-----------------------------------------------+--------------+
| subscriber_fields_id | name | field_type |
+----------------------+-----------------------------------------------+--------------+
| 143 | Peshawar/Islamabad/Lahore/Swat/Mardan/Karachi | Job Location |
| 152 | Islamabad or Lahore | Job Location |
| 155 | Islamabad | Job Location |
+----------------------+-----------------------------------------------+--------------+
It should return all rows where the name includes Islamabad, Khairpur or Karachi but it is not.
解决方案
For a proper solution, either normalize your database design or, barring that, consider full text search.
For a quick solution to the problem at hand, use a regular expression match (~
) or three simple LIKE
expressions:
SELECT *
FROM subscriberfields
WHERE name ~ '(Khairpur|Islamabad|Karachi)';
Or:
...
WHERE (name LIKE '%Khairpur%' OR
name LIKE '%Islamabad%' OR
name LIKE '%Karachi%')
Or use ~*
or ILIKE
for case-insensitive matching.
Since another answer suggested it: never use SIMILAR TO
:
- Using SIMILAR TO for a regex?
- Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
这篇关于SQL查询以匹配多个字符串之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!