Wenn Sie MySQL 8.0 ausführen, beachten Sie Folgendes:
SELECT *
FROM mytable
ORDER BY
FLOOR((ROW_NUMBER() OVER(PARTITION BY type ORDER BY timestamp) - 1)/3),
type,
timestamp
| id | type | timestamp |
| --- | ---- | --------- |
| 1 | A | 101 |
| 2 | A | 102 |
| 5 | A | 105 |
| 3 | B | 103 |
| 4 | B | 104 |
| 6 | B | 106 |
| 7 | A | 107 |
| 8 | A | 108 |
| 10 | A | 110 |
| 9 | B | 109 |
| 11 | B | 111 |
| 12 | B | 112 |
In früheren Versionen können Sie Variablen verwenden, um ROW_NUMBER()
zu emulieren :
SELECT id, type, timestamp
FROM (
SELECT
t.*,
@rn := CASE WHEN @type = type THEN @rn + 1 ELSE 1 END rn,
@type := type
FROM
mytable t
CROSS JOIN (SELECT @type := NULL, @rn := 1) x
ORDER BY type, timestamp
) x
ORDER BY
FLOOR((rn - 1)/3),
type,
timestamp;