我们想要根据搜索字符串从表中搜索记录。
此搜索是存储过程的一部分。
以下是我们需要的:
1.表ppl中有列name,其中包含格式为name,surname的数据
说记录是
巴拉克·奥巴
酒吧,奥巴马
还有巴拉克、奥巴马。
- 我们想要根据搜索字符串选择记录。搜索字符串可以是姓名或姓氏。
-
我们希望根据搜索字符串进行精确搜索。
表示搜索字符串是否为 bar;它不应该返回 barack,obama 记录,而应该只返回 bar,obama。
对姓氏部分进行类似的精确搜索。
-
我们不知道搜索字符串是否包含姓名。
所以搜索字符串甚至可以是“oba”..并且它应该只返回“barack,oba”记录。
以下是我们迄今为止尝试过的方法;但这些都不起作用:
- 从 ppl 中选择名称,其中名称如“bar,%”或名称如“%,bar”;
这适用于姓名部分,但不适用于姓氏部分。
从 ppl 中选择名称,其中名称如“oba,%”或名称如“%,oba”;
没有给出正确的结果。
-
从 ppl 中选择名称,其中 name = ANY('%,oba',oba,%'); - 不工作
select name from ppl where regexp_like (name,'^,oba$') OR regexp_like (name,'oba$,^'); - 不工作
我请求您分享您对这种情况的想法。
We want to search a record from table based on search string.
This search is a part of Stored procedure.
Following is what we need:
1. In table ppl there is column name which contains data in format of name,surname
Say records are
barack,oba
bar,obama
and barack,obama.
- We want to select record based on search string. Search string can be name OR surname.
-
We want exact search based on search string.
Means if search string is bar; it should not return barack,obama record and should only return only bar,obama.
Similar exact search for surname part.
-
We don’t know if the search string will have name or surname.
So search string can even be “oba” ..and it should return only “barack,oba” record.
Following approaches we have tried untill now; but none of these worked :
- select name from ppl where name like 'bar,%' or name like '%,bar';
This is working for name part but not working for surname part.
select name from ppl where name like 'oba,%' or name like '%,oba';
is not giving proper results.
-
select name from ppl where name = ANY('%,oba',oba,%'); - not working
-
select name from ppl where regexp_like (name,'^,oba$') OR regexp_like (name,'oba$,^'); - not working
I request you to share your thoughts in this case.
发布评论
评论(2)
您需要使用正则表达式(REGEX)。
有许多站点描述了如何使用它们,例如:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
http://www.dba-oracle.com/t_regular_expressions.htm
http: //orafaq.com/node/2404
对于您所描述的示例(因为这就是我必须继续进行的),您可以使用:
以编程方式替换 v_name 为您的名称字符串。如果要不区分大小写地搜索名称字符串,则:
编辑:您可能可以使用 REGEXP_LIKE 比 REGEXP_INSTR 更好,但我有类似于 hand 的东西。
例如,不区分大小写:
区分大小写:
希望这有帮助...
You need to use a regular expression (REGEX).
There are many sites that describe how to use them for example:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
http://www.dba-oracle.com/t_regular_expressions.htm
http://orafaq.com/node/2404
For the examples you have described (as that's all i have to go on) you could use:
Programatically substituting v_name for your name string. If the name string is to be searched case insensitively then:
EDIT: You could probably use REGEXP_LIKE better than REGEXP_INSTR but I had somethig similar to hand.
e.g. Case insensitive:
Case sensitive:
Hope this helps...
根据示例数据和 SQL 你的问题,我看不出你有问题:
首先我复制了你的表和数据(使用 SQL Plus):
现在你的查询:
这是正确的。
这也是正确的。
那么你会得到什么结果呢?
Based on the sample data and SQL your question I can't see that you have a problem:
First I replicated your table and data (using SQL Plus):
Now your queries:
That's correct.
That's also correct.
So what results do you get?