如何从每个组中选择MySql一个随机行

时间:2020-08-04 02:14:42

标签: mysql random group-by

我需要从每只猫中随机选择一行。我使用以下数据创建了示例MySql查询;

select 1 id, 1 cat from dual
union all select 2 id, 1 cat from dual
union all select 3 id, 1 cat from dual
union all select 4 id, 2 cat from dual
union all select 4 id, 2 cat from dual
union all select 4 id, 3 cat from dual;

我需要从cat 1中返回一个随机选择的行,从cat 2中返回另一行,依此类推。

我可以使用rand()函数创建随机字段,但是如何使用组或其他方法来获得期望的结果?这里其他类似的帖子也没有解决我所寻找的问题。

我必须避免存储过程,函数或繁重的查询,因为我要处理大量数据。任何想法将不胜感激。

0 个答案:

没有答案