将多个SQL语句合并为一个

0 mysql sql

如何在单个SQL语句中组合这些语句?

select city from station where city like 'a%';

select city from station where city like 'e%';

select city from station where city like 'i%';

select city from station where city like 'o%';

select city from station where city like 'u%'; 
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 5

显而易见的方法是union all:

select city from station where city like 'a%' union all
select city from station where city like 'e%' union all
. . . 
Run Code Online (Sandbox Code Playgroud)

也许几乎同样明显的是:

where city like 'a%' or city like 'e%' or . . . 
Run Code Online (Sandbox Code Playgroud)

您还可以使用其他方法,例如:

where left(city, 1) in ('a', 'e', 'i', 'o', 'u')
Run Code Online (Sandbox Code Playgroud)

(注意:并非所有数据库都支持left().)

更典型的方法是某种形式的正则表达式,但该语法取决于数据库.