gpt4 book ai didi

mysql - SQL查询分组按日期洗牌

转载 作者:行者123 更新时间:2023-11-29 13:29:55 26 4
gpt4 key购买 nike

我有一个类似的查询

select id, item, date producer from table

结果是这样的:

id         item            producer            date
1 apple A 2013-10-26
2 pear A 2013-10-26
3 peach A 2013-10-26
4 orange B 2013-10-26
5 strawberry B 2013-10-27
6 melon B 2013-10-27
7 apple2 A 2013-10-27
8 orange3 A 2013-10-27

我需要打乱这些数据“order by DATE DESC”并得到如下内容:

item            producer
orange3 A
melon B
apple2 A
strawberry B
pear A
orange B
apple A
peach A
melon B

我不想这样显示:

所有项目...所有 B 项...或者将今天添加的内容与昨天添加的内容混在一起...在我的示例中,我不想在“orange3”之前显示“orange”

我的解决方案(但很慢)

Select * from table where date = $date order by rand;
Select * from table where date = $date -1 order by rand;
select * from table where date = $date -2 order by rand;

(这只是一个概念,$date不能用这种方法递减)

最佳答案

看看以下内容是否适合您:

SELECT * 
FROM `table`
ORDER BY `date` DESC, RAND()

我刚刚尝试过,它似乎可以满足我的要求。

编辑

如果您想保留“随机播放”,请添加名为 rnd 的 DOUBLE 类型列,执行

UPDATE `table` SET rnd = RAND()

然后在 SELECT 语句中使用

.... ORDER BY `date` DESC, `rnd`

关于mysql - SQL查询分组按日期洗牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19621360/

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