我有一个工作查询,我需要重复几次,但是我在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查询只是用于测试目的的原始查询的重复)
答案 0 :(得分:2)
你有一些额外的括号。
请参阅此SQL Fiddle
答案 1 :(得分:1)
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;
答案 2 :(得分:1)
删除最外面的括号。 SELECT语句应该以SELECT关键字开头,而不是一个开放的paren。