HQL 随机抽取N条记录 怎么写 用的MYSQL数据库
2016-01-15
展开全部
SELECT
group_code,
name
FROM
(
SELECT
CASE
WHEN @cn != group_code THEN @rownum:= 1
ELSE @rownum:= @rownum + 1
END AS No,
group_code,
@cn := group_code AS group_code,
name
FROM
(SELECT @rownum:=0) r,
(SELECT @cn:= '') p,
(SELECT * FROM test_order_by_rand order by group_code, rand()) tmp
) subQuery
WHERE
No <= 3
ORDER BY
group_code;
group_code,
name
FROM
(
SELECT
CASE
WHEN @cn != group_code THEN @rownum:= 1
ELSE @rownum:= @rownum + 1
END AS No,
group_code,
@cn := group_code AS group_code,
name
FROM
(SELECT @rownum:=0) r,
(SELECT @cn:= '') p,
(SELECT * FROM test_order_by_rand order by group_code, rand()) tmp
) subQuery
WHERE
No <= 3
ORDER BY
group_code;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询