Need to select people whose name starts with "M" or the second from the end letter of the last name is "i"? The column has persons name, surname

huangapple go评论63阅读模式
英文:

Need to select people whose name starts with "M" or the second from the end letter of the last name is "i"? The column has persons name, surname

问题

我在SQL中的表中有一列包含人的名字和姓氏,例如"Mike Baldwin"、"Sara Larson"等,我需要输出所有名字以"M"开头或姓氏倒数第二个字母是"i"的人。我应该如何在脚本文件中编写它?

所以我制作了这个脚本:

SELECT * FROM your_table WHERE CLIENT LIKE 'M%' OR CLIENT LIKE '%i_'

但这个脚本输出了名字的倒数第二个字母也是"i"的人。

英文:

My table in SQL has a column with persons name and surname like "Mike Baldwin", "Sara Larson" etc. and need to output all the persons whose name starts with "M" or surnames second last letter is "i". How do I write it in script file?

So I made this script :

SELECT * WHERE CLIENT LIKE 'M%' OR CLIENT LIKE '%i_'

but this script outputs persons whose second last letter of the name is also "i".

答案1

得分: 0

根据我理解;namesurname 是根据您的解释两个不同的列;

select *
from table_name
where name like 'M%' OR surname like '%i_';

应该可以工作。

请在上面的查询中替换实际的列名

============================================================

更新:
根据解释;
有一个单独的列 client,其中存储了通过空格分隔的 namesurname

select *
from table_name
where SUBSTR(client, 1, 1) = 'M'
   OR SUBSTR(client, INSTR(client, ' ', -1) - 1, 1) = 'i';
英文:

As I understand; name and surname are two different columns based on your explanation;

select *
from table_name
where name like'M%' OR surname like '%i_';

Shall work.

Please replace the actual column names in above query

============================================================

Update :
Based on explanation;
There a single column client which stores name and surname seperated by a space

select *
from table_name
where SUBSTR(client, 1, 1) = 'M'
   OR SUBSTR(client, INSTR(client, ' ', -1) - 1, 1) = 'i';

答案2

得分: 0

只返回翻译好的部分:

"Thinking use some Regex here. Something like but I don't know how regex works in oracle?

SELECT *
FROM table_name
WHERE CLIENT LIKE 'M%' OR
REGEX_LIKE (CLIENT , '^[a-zA-Z]+\s.[i]+[a-zA-Z]*$')

Updated the regex!"

英文:

Thinking use some Regex here. Something like but I don't know how regex works in oracle?

SELECT *
FROM table_name
 WHERE CLIENT LIKE 'M%' OR 
REGEX_LIKE (CLIENT , '^[a-zA-Z]+\s.[i]+[a-zA-Z]*$')

Updated the regex!

huangapple
  • 本文由 发表于 2023年3月4日 00:22:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定