我有一个需要重复几次的有效查询,但是我在 UNION ALL 上遇到语法错误:
工作查询:
set @num := 0, @group := '';
select person, `group`, age
from
(
select person, `group`, age,
@num := if(@group = `group`, @num + 1, 1) as row_number,
@group := `group` as dummy
from mytable
order by `Group`, Age desc
) as x where x.row_number <= 2;
UNION ALL 失败(出现语法错误):
set @num := 0, @group := '';
(
select person, `group`, age
from
(
select person, `group`, age,
@num := if(@group = `group`, @num + 1, 1) as row_number,
@group := `group` as dummy
from mytable
order by `Group`, Age desc
) as x where x.row_number <= 2;
)
UNION ALL
(
select person, `group`, age
from
(
select person, `group`, age,
@num := if(@group = `group`, @num + 1, 1) as row_number,
@group := `group` as dummy
from mytable
order by `Group`, Age desc
) as x where x.row_number <= 2;
)
(出于测试目的,UNIONed 查询只是原始查询的副本)