gpt4 book ai didi

mysql - 选择随机日期范围

转载 作者:行者123 更新时间:2023-11-29 07:06:37 24 4
gpt4 key购买 nike

我正在尝试从具有随机日期范围的表格中进行选择。有点像

SELECT * FROM my_table WHERE timestamp 
BETWEEN <random_date> AND <the_same_random_date> - INTERVAL 1 DAY

这样我应该选择一个随机日期的 1 天间隔。有什么建议吗?

更新:因此随机查询可能与以下查询具有相同的效果:

SELECT * FROM my_table WHERE timestamp 
BETWEEN '2011-08-05 00:00' AND '2011-08-05 00:00' - INTERVAL 1 DAY

SELECT * FROM my_table WHERE timestamp
BETWEEN '2011-08-12 14:30' AND '2011-08-12 14:30' - INTERVAL 1 DAY

SELECT * FROM my_table WHERE timestamp
BETWEEN '2011-05-11 06:15' AND '2011-05-11 06:15' - INTERVAL 1 DAY

SELECT * FROM my_table WHERE timestamp
BETWEEN '2011-03-03 21:11' AND '2011-03-03 21:11' - INTERVAL 1 DAY

最佳答案

SELECT * 
FROM
my_table
CROSS JOIN (SELECT FROM_UNIXTIME(RAND() * 2147483647) random_date) rd
WHERE
timestamp BETWEEN (rd.random_date - INTERVAL 1 DAY) AND rd.random_date

关于mysql - 选择随机日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6957097/

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