数回繰り返す必要がある作業クエリがありますが、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 クエリは、テスト目的で元のクエリを複製したものです)