Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION

旧城冷巷雨未停 提交于 2020-07-23 09:21:05

问题


Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION.

My answer/tried code is:

select city from station where REGEXP_LIKE(city,'[^aeiou]+');

But it doesn't seem to be correct.

Kindly help me with this.


回答1:


As BackSlash have already commented, you've written the wrong REGEXP_LIKE pattern and you should change it to '^[aeiou].+', or you can even ommit .+ from your pattern, as you're only interested in the first letter of your string (containing more than 1 character):

select city from station where REGEXP_LIKE(city,'^[aeiou]');

Example with test data

Beware that would only return stations that start with lowercase vowels! If you also want to include uppercase vowels than add them to your pattern:

select city from station where REGEXP_LIKE(city,'^[aeiouAEIOU]');

or specify inside REGEXP_LIKE call that inputted pattern is case-insensitive with an 'i' flag, like this:

select city from station where REGEXP_LIKE(city,'^[aeiou]', 'i');

Example with test data

Kudos to MT0 for helpful comment!

I hope we helped!




回答2:


use SUBSTR

select t.city from station t where lower(SUBSTR(city,1,1)) in ('a','e','i','o','u')



回答3:


Try with MySQL solution:

select distinct CITY from STATION where substr(CITY,1,1) in ('a','e','i','o','u');

Here "distinct" will solve the problem of duplicate value and "substring" function extract substring from string . Substring also contain start & length . For more details follow the link :- https://www.w3schools.com/sql/func_mysql_substr.asp




回答4:


That worked for me

SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[aeiou]'


来源:https://stackoverflow.com/questions/54920226/query-the-list-of-city-names-starting-with-vowels-i-e-a-e-i-o-or-u-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!