3
4

4 回答 4

7

Assuming you're asking for two a characters search for a string with two a's but not with three.

select *
  from people
 where names like '%a%a%'
   and name not like '%a%a%a%'
于 2013-10-07T20:33:23.200 回答
4

Use '_a'. '_' is a single character wildcard where '%' matches 0 or more characters.

If you need more advanced matches, use regular expressions, using REGEXP_LIKE. See Using Regular Expressions With Oracle Database.

And of course you can use other tricks as well. For instance, you can compare the length of the string with the length of the same string but with 'a's removed from it. If the difference is 2 then the string contained two 'a's. But as you can see things get ugly real soon, since length returns 'null' when a string is empty, so you have to make an exception for that, if you want to check for names that are exactly 'aa'.

select * from People
where
  length(Names) - 2 = nvl(length(replace(Names, 'a', '')), 0)
于 2013-10-07T20:32:37.717 回答
1

Another solution is to replace everything that is not an a with nothing and check if the resulting String is exactly two characters long:

select names 
from people
where length(regexp_replace(names, '[^a]', '')) = 2;

This can also be extended to deal with uppercase As:

select names 
from people
where length(regexp_replace(names, '[^aA]', '')) = 2;

SQLFiddle example: http://sqlfiddle.com/#!4/09bc6

于 2013-10-07T20:39:46.047 回答
-1

select * from People where names like '__'; also ll work

于 2017-04-14T10:37:21.373 回答