从表格中选择不以元音开头和不以元音结尾的城市

时间:2019-05-15 06:34:24

标签: sql-server

从STATION查询不以元音开头也不以元音结尾的CITY名称列表。您的结果不能包含重复项。

Station表的列名为

id (int), 
city varchar(21),
state varchar2(2) 

解决方案:

select distinct city from station 
Except
select distinct city from station where city like '[aeiou]%[aeiou]'

OR

select distinct city from station where city not like '[aeiou]%[aeiou]'

两个查询都不起作用。有人可以就这个问题提供意见吗?

6 个答案:

答案 0 :(得分:2)

在这种情况下,使用wildcard character not match会有所帮助。

以下查询将返回未以元音开头和结尾的城市名称。

SELECT DISTINCT city FROM station 
WHERE (LOWER(city) LIKE '[^aeiou]%' AND LOWER(city) LIKE '%[^aeiou]');

演示,其中包含一些示例数据:

DECLARE @Station TABLE (City VARCHAR (50));

INSERT INTO @Station (City) VALUES
('Abced'), ('EeepA'), ('CongE'), ('RaaaR'), ('KeeeK');

SELECT DISTINCT city FROM @station 
WHERE (LOWER(city) LIKE '[^aeiou]%' AND LOWER(city) LIKE '%[^aeiou]');

输出:

city
-----
KeeeK
RaaaR

Demo on db<>fiddle

答案 1 :(得分:1)

尝试使用MySQL解决方案:

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

这里“ distinct”将解决重复值的问题,“ substring”函数从string中提取子字符串。子字符串还包含开始和长度。 有关更多详细信息,请单击链接:-https://www.w3schools.com/sql/func_mysql_substr.asp

答案 2 :(得分:0)

这对于使用MySQL的我来说是有效的:

select distinct CITY from STATION where CITY NOT RLIKE '^[aeiouAEIOU]' AND CITY NOT RLIKE '[AEIOUaeiou]$' GROUP BY CITY;

答案 3 :(得分:0)

SELECT DISTINCT CITY
FROM STATION
WHERE CITY RLIKE '^[^aieouAEOIU]' OR CITY RLIKE  '[^aieouAEOIU]$'

答案 4 :(得分:0)

对于oracle,请尝试以下操作:

SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'^.*[^aeiouAEIOU]$') 
INTERSECT
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'^[^aeiouAEIOU].*$');

答案 5 :(得分:-1)

   select distinct city from station 
where city not in(
select distinct city from station where city like '[aeiou]%')