A
SELECT MAX(
id
) as id, item_id
FROM comments WHERE stat
=1 GROUP BY item_id
ORDER BY id DESCSize: a a a
A
id
) as id, item_id
FROM comments WHERE stat
=1 GROUP BY item_id
ORDER BY id DESCA
id
) as id, item_id
FROM comments GROUP BY item_id
ORDER BY id DESCСШ
ПД
A
ПД
СШ
А
WITH d AS (
SELECT 1 id, 'comment10' comment, 1 item_id
UNION ALL SELECT 2, 'comment1', 1 val
UNION ALL SELECT 3, 'comment3', 2 val
UNION ALL SELECT 4, 'comment4', 2 val
)
, t AS (
SELECT id, comment, item_id
, ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY id DESC) rn
FROM d
)
SELECT id, comment, item_id
FROM t
WHERE rn = 1
ORDER BY id DESC
LIMIT 20
A
WITH d AS (
SELECT 1 id, 'comment10' comment, 1 item_id
UNION ALL SELECT 2, 'comment1', 1 val
UNION ALL SELECT 3, 'comment3', 2 val
UNION ALL SELECT 4, 'comment4', 2 val
)
, t AS (
SELECT id, comment, item_id
, ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY id DESC) rn
FROM d
)
SELECT id, comment, item_id
FROM t
WHERE rn = 1
ORDER BY id DESC
LIMIT 20
A
А
А
ls
A
stat
=1 GROUP BY item_id ORDER BY id DESC LIMIT 20 А
A
stat
=1 - начинает тормозить