WITH RECURSIVE numbers AS ( SELECT 1 n UNION ALL SELECT n + 1 FROM numbers WHERE n < ( SELECT MAX(stickers) FROM buy WHERE entryFk = ? ) ), labels AS ( SELECT ROW_NUMBER() OVER(ORDER BY b.id, num.n) labelNum, i.name, i.`size`, i.category, ink.id color, o.code, b.packing, b.`grouping`, i.stems, b.id, b.itemFk, p.name producer, IFNULL(i2.comment, i.comment) comment FROM buy b JOIN item i ON i.id = b.itemFk LEFT JOIN producer p ON p.id = i.producerFk LEFT JOIN ink ON ink.id = i.inkFk LEFT JOIN origin o ON o.id = i.originFk LEFT JOIN item i2 ON i2.id = b.itemOriginalFk JOIN numbers num WHERE b.entryFk = ? AND num.n <= b.stickers ) SELECT *, (SELECT SUM(stickers) FROM buy WHERE entryFk = ?) maxLabelNum FROM labels WHERE id = ?