[英]oracle sql how to select specific words
This is my school work. 这是我的学校作业。 Although the professor said 'Don't spend too much time on it' as it is said to be a brain teaser, I would like to try to solve it. 尽管教授说“不要花太多时间”,因为据说这是个脑筋急转弯,但我想尝试解决它。 However I am still way to go. 但是,我仍然任重道远。
Return all the contact names, and contact title for all customers whose contact title has "Sales" as the 2nd word of the title, the following examples are in the current data: 返回所有联系人姓名,以及所有联系人标题为“ Sales”的所有单词的所有客户的联系人标题,以下示例包含在当前数据中:
- Associate Sales Assistant - should be returned 助理销售助理-应该退货
- Associate Salesmanager - should not be returned 助理销售经理-不应退货
- Manager Sales - should be returned 经理销售-应该退货
- Assistant to Sales Manager - should not be returned 销售经理助理-不应退货
A simplistic approach using LIKE: 使用LIKE的简单方法:
SELECT * FROM Customers WHERE
--Sales is a word on its own i.e. preceded and followed by a space
--or Sales is a word on its own, at the end of the title
(ContactTitle LIKE '% Sales %' OR ContactTitle LIKE '% Sales') AND
--and there is only one space before the word sales
NOT ContactTitle LIKE '% % Sales%'
This is a way: 这是一种方法:
with sampleData(col) as (
select 'Associate Sales Assistant' from dual union all
select 'Associate Salesmanager' from dual union all
select 'Manager Sales' from dual union all
select 'Assistant to Sales Manager' from dual
)
select col
from sampleData
where regexp_like(col, '^[A-Za-z]+ Sales( |$)')
How it works: 这个怎么运作:
^
: the beginning of the string ^
:字符串的开头 [A-Za-z]
uppercase or lowercase letters; [A-Za-z]
大写或小写字母; if your "words" may contains some other character, you simply have to add it in brackets; 如果您的“单词”可能包含其他字符,则只需将其添加在方括号中即可; for example, if 'aa_bb' is a valid word, this part should become [A-Za-z_]
例如,如果“ aa_bb”是有效字词,则此部分应变为[A-Za-z_]
+
one or more occurrences of the preceding part +
前一部分的一个或多个事件 Sales
a space followed by 'Sales' Sales
空间,后跟“销售” ( |$)
a space or the end of the string ( |$)
空格或字符串的结尾 You can get the same result with different patterns ; 您可以使用不同的模式获得相同的结果; I believe this one is quite clear 我相信这很清楚
Combination of substr() and instr() is what I'd do. substr()和instr()的组合是我要做的。 I hate using REGEXP unless there is no other way to do it. 我讨厌使用REGEXP,除非没有其他方法可以这样做。
This should get you close... You'll need a couple more where clauses I think. 这应该使您接近……我需要更多一些where子句。 Another instr to find the second word and make sure it is 'Sales', and another to verify after 'Sales' it is either a space or no characters. 另一个指令找到第二个单词并确保它是“ Sales”,另一个在“ Sales”之后验证它是空格还是没有字符。
WHERE substr(title_col, instr(title_col,' Sales')+1,5) = 'Sales'
Edit: after seeing the REGEXP solution from Aleksej, that does seem more readable and simpler. 编辑:看到来自Aleksej的REGEXP解决方案后,这似乎更易于阅读和简化。 If you wanted to use substr and instr, you'll need more where clauses to handle the different cases 如果要使用substr和instr,则需要更多的where子句来处理不同的情况
Edit2: I like the solution from Caius Jard the best. Edit2:我最喜欢Caius Jard的解决方案。 Simple and readable. 简单易读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.