更简单的查询?

kgqe7b3p  于 2021-07-24  发布在  Java
关注(0)|答案(4)|浏览(279)
SELECT DISTINCT CITY FROM STATION WHERE City LIKE 'a%'
Union
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE 'e%'
Union
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE 'i%'
Union
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE 'o%'
Union
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE 'u%';
isr3a4wc

isr3a4wc1#

你会用 regexp :

select distinct city
from station
where city regexp '^[aeiou]';
63lcw9qa

63lcw9qa2#

你可以用 OR 为了这个。

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%';
qkf9rpyu

qkf9rpyu3#

sql where子句可以有多个条件:

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%';

此外,由于您只关心以开头,因此可以切换到substr匹配:

SELECT DISTINCT city
FROM station
WHERE substr(city,1,1) IN ('a','e','i','o','u');

尽管注意如果 city 上面有索引这可能性能较差(需要全表扫描)

2g32fytz

2g32fytz4#

在下列情况下,我们可以使用多个或:

SELECT DISTINCT CITY
    FROM STATION
    WHERE CITY LIKE 'u%'
      OR CITY LIKE 'e%'
      OR CITY LIKE 'a%'
      OR CITY LIKE 'i%'
      OR CITY LIKE 'o%';

相关问题