gpt4 book ai didi

MySQL order by rand() 按天分组

转载 作者:可可西里 更新时间:2023-11-01 07:47:54 26 4
gpt4 key购买 nike

是否有可能在同一天内获得随机元素?

例如:

+----+---------------------+  | id | md                  |  +----+---------------------+  |  1 | 2010-06-27 11:26:01 |  |  2 | 2010-06-27 11:28:20 |  |  3 | 2010-06-27 11:29:46 |  |  4 | 2010-06-27 11:30:50 |  |  5 | 2010-06-27 12:20:56 |  |  6 | 2010-06-27 12:27:42 |  |  7 | 2010-06-27 15:14:05 |  |  8 | 2010-07-06 01:53:33 |  |  9 | 2010-07-06 01:52:52 |  +----+---------------------+  

我想在同一天随机挑选元素,但同时我想按日期降序排列。像这样:

+----+---------------------+  | id | md                  |  +----+---------------------+  |  8 | 2010-07-06 01:53:33 |  random block  |  9 | 2010-07-06 01:52:52 |  |  2 | 2010-06-27 11:28:20 |  random block  |  4 | 2010-06-27 11:30:50 |  |  1 | 2010-06-27 11:26:01 |  |  6 | 2010-06-27 12:27:42 |  |  3 | 2010-06-27 11:29:46 |  |  5 | 2010-06-27 12:20:56 |  |  7 | 2010-06-27 15:14:05 |  +----+---------------------+  

不知道如何开始,或者这是否可能,因为 rand() 的排序不接受分组。

最佳答案

非常简单:

SELECT * 
FROM tbl
ORDER BY md DESC, RAND()

关于MySQL order by rand() 按天分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3188921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com