mysql从每个组中选择2行

mysql从每个组中选择2行,第1张

mysql从每个组中选择2行

SELECt p.id, g.typeid, g.id, g.title
FROM products p
INNER JOIN (SELECt * FROM gallery a
WHERe (SELECt COUNT(*) FROM gallery b WHERe b.title = a.title AND b.id >= a.id) <= 2
) g ON p.id = g.typeid
WHERe p.id in (3,4,5)


编辑:

试试这个SQL小提琴演示

SELECt p.id, g.typeid, g.id, g.nameFROM products p INNER JOIN (SELECt * FROM gallery a WHERe (SELECt COUNT(*) FROM gallery b WHERe b.typeid = a.typeid AND b.id >= a.id) <= 2) g ON p.id = g.typeid WHERe p.id in (3,4,5) order by g.id asc

所以基本上这部分

WHERe (SELECt COUNT(*) FROM gallery b WHERe b.title = a.title AND b.id >= a.id) <= 2

用于替换

group by typeid



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5675311.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存