我不能使用 INTERSECT 运算符

I cannot use the INTERSECT operator

我正在尝试解决这个练习:

'Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.'

我尝试像这样使用 INTERSECT 运算符解决它:

SELECT CITY 
FROM STATION 
WHERE LEFT(CITY, 1) = 'A' OR LEFT(CITY, 1) = 'O'
OR LEFT(CITY, 1) = 'E' OR LEFT(CITY, 1) = 'I' OR
LEFT (CITY, 1) OR LEFT(CITY, 1) = 'U'

INTERSECT

SELECT CITY
FROM STATION
WHERE RIGHT(CITY, 1) = 'A' OR RIGHT(CITY, 1) = 'O'
OR RIGHT(CITY, 1) = 'E' OR RIGHT(CITY, 1) = 'I' OR
RIGHT (CITY, 1) OR RIGHT(CITY, 1) = 'U';

但我收到以下错误信息:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERSECT
SELECT CITY
FROM STATION
WHERE RIGHT(CITY, 1) = 'A' OR RIGHT(CITY, 1)' at line

我做错了什么,还有什么其他方法可以解决这个问题?

您可以使用 join:

SELECT CITY1 AS CITY
FROM
(
    SELECT CITY AS CITY1
    FROM STATION 
    WHERE LEFT(CITY, 1) = 'A' OR LEFT(CITY, 1) = 'O'
    OR LEFT(CITY, 1) = 'E' OR LEFT(CITY, 1) = 'I' OR
    LEFT (CITY, 1) OR LEFT(CITY, 1) = 'U'
) AS T1 JOIN (
    SELECT CITY AS CITY2
    FROM STATION
    WHERE RIGHT(CITY, 1) = 'A' OR RIGHT(CITY, 1) = 'O'
    OR RIGHT(CITY, 1) = 'E' OR RIGHT(CITY, 1) = 'I' OR
    RIGHT (CITY, 1) OR RIGHT(CITY, 1) = 'U'
) AS T2 ON CITY1 = CITY2

Query the list of CITY names from STATION which have vowels as both their first and last characters.

你似乎把这个复杂化了。据我了解这个问题,你只需要一个 where 过滤 table 一次的子句 - 正则表达式可以派上用场:

select city
from station
where city regexp '^[aeiou].*[aeiou]$'

该模式描述了一个字符串,它以一个 wovel 开始和结束(^ 代表字符串的开始,$ 是结束)。

如果 table 中有重复的 city,请改用 select distinct city from ...

如果你想使用left()right():

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