All:
I am learnig SQL now, but stuck at #7 of
http://sqlzoo.net/wiki/SELECT_names
Bahamas has three a - who else?
Find the countries that have three or more a in the name
Thanks
Try using the LIKE
operator:
SELECT name FROM world
WHERE name LIKE '%a%a%a%'
If you want to do case-insensitive search for either a
or A
then you can use the LOWER()
function:
SELECT name FROM world
WHERE LOWER(name) LIKE '%a%a%a%'
Edit:
We could also use REGEXP
here:
SELECT name FROM world
WHERE name REGEXP '(.*[a]){3,}';
However, for this particular example, I would go with LIKE
, because it probably would perform better, and less of an overhead, than using REGEXP
.
Try this query:
SELECT name
FROM world
WHERE LEN(name) - LEN(REPLACE(name,'a', '')) > 2
I'm trying to solve this lecture with regular expressions but couldn't match this correctly.
regexp '(.*[a]){3,}'
Should match 3 or more 'a', which it does but:
"Sao Tomé and Príncipe" with 2 a's is matched by LIKE '%a%a%a%'
which is the correct answer.
Im curious what's going wrong here?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With