यदि आप MySQL 8.0 चला रहे हैं, तो इस पर विचार करें:
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 |
पुराने संस्करणों में, आप ROW_NUMBER()
. का अनुकरण करने के लिए चरों का उपयोग कर सकते हैं :
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;