标签:mysq from col desc gid sql sele esc 分组
取GID每组 COL2最大的3条记录:
SELECT a.id,a.gid,a.col1,a.col2 FROM t2 a
WHERE 3>=(
SELECT COUNT(*) FROM t2 b
WHERE a.gid=b.gid AND a.col2<=b.col2)
ORDER BY a.gid,a.col2 desc
或
SELECT a.id,a.gid,a.col1,a.col2 FROM t2 a
LEFT JOIN t2 b
ON a.gid=b.gid AND a.col2<=b.col2
GROUP BY a.id,a.gid,a.col1,a.col2
HAVING COUNT(b.id)<=3
ORDER BY a.gid,a.col2 desc
标签:mysq from col desc gid sql sele esc 分组
原文地址:http://www.cnblogs.com/SZxiaochun/p/7755854.html