提问者:小点点

使用了两次限制的mysql选择联合[重复]


我有示例查询

select id from clients where email like '%a%'
union 
select id from clients where email like '%b%'

这很好,但我需要增加一些限制,比如

select id from clients where email like '%a%' limit 5
union 
select id from clients where email like '%b%' limit 5

但这与

#1064 - 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 'union
select id from clients where email like '%b%' limit 5' at line 2

我做了一些尝试,令人惊讶的是

select id from clients where email like '%a%' limit 5
union 
select id from clients where email like '%b%'

工作良好

我使用的是oracle MySQL8。 知道我做错了什么吗?


共1个答案

匿名用户

您可以使用子查询

select id from  (
  select id from clients where email like '%a%' limit 5
) t
union 
select id  from  (
    select id from clients where email like '%b%' limit 5
) t