SQL Server 不以元音开头也不以元音结尾的

x3naxklr  于 2023-02-03  发布在  其他
关注(0)|答案(9)|浏览(197)

我正在练习Hackerrank上的一个挑战,问题是从表“STATION”返回所有城市的列表,其中城市不以元音开始,也不以元音结尾。
此查询不起作用:

select distinct CITY from STATION where CITY NOT LIKE '[A,E,I,O,U]% 
[a,e,i,o,u]'

但是这个查询确实有效:

select distinct CITY from STATION where CITY NOT LIKE '[A,E,I,O,U]%' 
AND CITY NOT LIKE '%[a,e,i,o,u]'

有人能帮我理解为什么第一个查询不起作用吗?

wpx232ag

wpx232ag1#

您的第二个查询可以工作--因为逗号不会出现在名称的开头或结尾。
在所有的可能性。所需的解决方案是一个单一的模式:

where city like '[^aeiou]%[^aeiou]'
t3psigkw

t3psigkw2#

select distinct city
from station
where city rlike '^[^aeiouAEIOU].*' and city rlike '^.*[^aeiouAEIOU]$'
hwamh0ep

hwamh0ep3#

试试这个:

SELECT DISTINCT CITY  FROM STATION  WHERE UPPER(SUBSTR(CITY, 1, 1)) NOT IN ('A', 'E', 'I', 'O', 'U')
     AND UPPER(SUBSTR(C.CITY, LENGTH(CITY), 1)) NOT IN ('A', 'E', 'I', 'O', 'U')
bejyjqdl

bejyjqdl4#

尝试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');
w7t8yxp5

w7t8yxp55#

SELECT DISTINCT City FROM Station WHERE City NOT LIKE '%A' AND City NOT LIKE '%E' AND City NOT LIKE '%I' AND City NOT LIKE '%O' AND City NOT LIKE '%U' AND City NOT LIKE 'A%' AND City NOT LIKE 'E%' AND City NOT LIKE 'I%' AND City NOT LIKE 'O%' AND City NOT LIKE 'U%' ORDER BY City
jgwigjjp

jgwigjjp6#

解决方案如下:

SELECT DISTINCT CITY FROM STATION 
WHERE (CITY NOT LIKE 'A%'
    AND CITY  NOT LIKE 'E%'
    AND CITY NOT LIKE 'I%' 
    AND CITY NOT LIKE 'O%'
    AND CITY NOT LIKE 'U%')
        AND (CITY NOT LIKE '%a' AND
            CITY  NOT LIKE '%e' AND
            CITY NOT LIKE '%i' AND 
            CITY NOT LIKE '%o' AND 
            CITY NOT LIKE '%u');
y53ybaqx

y53ybaqx7#

SELECT DISTINCT CITY 
FROM STATION 
WHERE LOWER(RIGHT(CITY,1)) NOT IN ('a', 'e', 'i', 'o', 'u') AND 
      LOWER(LEFT(CITY,1)) NOT IN ('a', 'e', 'i', 'o', 'u');
fquxozlt

fquxozlt8#

给予一下下面的代码

select distinct city from station
where left(city,1) not in ('a','e','i','o','u')
and right(city,1) not in ('a','e','i','o','u')
7bsow1i6

7bsow1i69#

使用基本的LIKE语句来选择第一个字母,在这里,我使用NOT LIKE来排除元音。

SELECT DISTINCT city
FROM station
WHERE LOWER(city) NOT LIKE 'a%'
AND LOWER(city) NOT LIKE 'e%'
AND LOWER(city) NOT LIKE 'i%'
AND LOWER(city) NOT LIKE 'o%'
AND LOWER(city) NOT LIKE 'u%';

相关问题