MySQL随机结果按顺序分组

编程入门 行业动态 更新时间:2024-10-09 15:24:02
本文介绍了MySQL随机结果按顺序分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我如何在group by group_id上获得随机结果?

How do i get a random result on group by group_id?

类似如下: MySQL中行组的随机顺序

这是我的小提琴: sqlfiddle/#!9/1c73d/3

不确定为什么它总是给我相同的结果.

not sure why it always gives me the same result.

CREATE TABLE job (`job_id` int, `group_id` int, `user_id` int, `title` varchar(50), `description` varchar(55), `type` tinyint) ; INSERT INTO job (`job_id`, `group_id`, `user_id`, `title`, `description`,`type`) VALUES (1, 1, 100, 'Title 1', 'Text 1', 1), (2, 1, 100, 'Title 2', 'Text 2', 1), (3, 1, 200, 'Title 3', 'Text 3', 1), (4, 1, 200, 'Title 4', 'Text 4', 1), (5, 1, 300, 'Title 5', 'Text 5', 2), (6, 1, 400, 'Title 6', 'Text 6', 1), (7, 1, 200, 'Title 7', 'Text 7', 1);

查询:

select * from job where type = 1 group by group_id order by rand()

推荐答案

假设您希望从type = 1下的每个组中获得一个随机记录:

Assuming you want one random record from each group under type = 1:

SELECT * FROM ( SELECT * FROM job WHERE type = 1 ORDER BY RAND() ) AS t GROUP BY t.group_id;

SQL FIDDLE DEMO

SQL FIDDLE DEMO

更多推荐

MySQL随机结果按顺序分组

本文发布于:2023-10-30 18:32:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!