I want to query the list of CITY
names from the table STATION(id, city, longitude, latitude)
which have vowels as both their first and last charact
Try this below code,
SELECT DISTINCT CITY
FROM STATIOn
WHERE city RLIKE '^[aeiouAEIOU].*.[aeiouAEIOU]$'
SELECT distinct CITY
FROM STATION
where (CITY LIKE 'a%'
OR CITY LIKE 'e%'
OR CITY LIKE 'i%'
OR CITY LIKE 'o%'
OR CITY LIKE 'u%'
) AND (CITY LIKE '%a'
OR CITY LIKE '%e'
OR CITY LIKE '%i'
OR CITY LIKE '%o'
OR CITY LIKE '%u'
)
SELECT DISTINCT city
FROM station
WHERE city RLIKE '^[^aeiouAEIOU]'OR city RLIKE'[^aeiouAEIOU]$'
You can try one simple solution for MySQL:
SELECT DISTINCT city FROM station WHERE city REGEXP "^[aeiou].*[aeiou]$";
You may try this
select city
from station where SUBSTRING(city,1,1) in ('A','E','I','O','U') and
SUBSTRING(city,-1,1) in ('A','E','I','O','U');
Use a regular expression.
WHERE name REGEXP '^[aeiou].*[aeiou]$'
^
and $
anchor the match to the beginning and end of the value.
In my test, this won't use an index on the name
column, so it will need to perform a full scan, as would
WHERE name LIKE 'a%a' OR name LIKE 'a%e' ...
I think to make it use an index you'd need to use a union of queries that each test the first letter.
SELECT * FROM table
WHERE name LIKE 'a%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'e%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'i%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'o%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'u%' AND name REGEXP '[aeiou]$'