我有我需要重複幾次工作查詢,但是我對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;
)
(在被聯合查詢只是出於測試目的,原始查詢的副本)
夠簡單 - 謝謝 – Yarin