è possibile simulare il partizionato row_number utilizzando variabili utente e quindi limitare le righe e applicare group_concat:
Si consideri la seguente tabella:
create table your_table (
id int primary key autoincrement,
category int,
value int
);
e dati:
insert into your_table (category, value)
values
(1, 1), (1, 2), (1, 3), (1, 4), (1, 5),
(2, 6), (2, 7), (2, 8), (2, 9), (2, 10),
(3, 11), (3, 12), (3, 13), (3, 14), (3, 15);
e vogliamo è il primo 3 (in ordine di ultimo id) valore per categoria concatenato:
select category,
group_concat(value order by id desc) as value_con
from (
select t.*,
@rn := if(@category = category, @rn + 1, if(@category := category,1, 1)) as seqnum
from your_table t
cross join (select @category := null, @rn := 0) x
order by t.category, t.id desc
) t
where seqnum <= 3
group by category;
uscita:
category value_con
1 5,4,3
2 10,9,8
3 15,14,13
Ecco una demo di questo.
aumento del valore group_concat_max_len in my.cnf – Omesh
può essere è possibile ottenere la risposta qui http://stackoverflow.com/questions/3378324/limit-ignored-in-query-with-group-concat http://stackoverflow.com/questions/23608464/group-concat-with-limit –