2010-07-06 71 views
3

是否有可能在同一天内获得随机项目?MySQL按rand()排序按天分组

例如:

 
+----+---------------------+ 
| 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()的命令将不接受分组。

+0

你只需要一个时间每返回日期?它看起来好像你想每次约会多次。 – Mike 2010-07-06 19:46:23

回答

3

也许不是很有效,但尽量

select * from (select * from tbl order by rand()) as t group by date(md) 
+0

感谢之前没有注意到:P – Rodrigo 2010-07-06 19:15:10

1

您可以使用WHERE子句...

WHERE date = 'insert date' ORDER BY rand() LIMIT 1` 

然后,只需使用PHP插入的任何日期是到字符串有每个日期。

或者只使用一个MySQL查询,只需按日期降序排列他们,和他们拉出来都使用PHP ...

while ($item = mysql_fetch_array($query)): 
    // process to figure out the date (I'm sure you can do that) 
    $mydates[$date][] = $id; // add that ID to that date array 
endwhile; 
foreach ($mydates as $date): 
    $key = array_rand($date); 
endforeach; 
+0

这有帮助,但我需要每天做一个查询 – Rodrigo 2010-07-06 18:23:52

+0

如果你做第二个选项,则不行。第二个你只是按日期排序,然后在PHP中完成所有的处理,这实际上会更快。 – animuson 2010-07-06 18:48:01

2

如果我已经正确理解你的问题,这应该做的伎俩:

SELECT * FROM table1 ORDER BY DATE(md) DESC, RAND(); 

例如:

DROP TABLE IF EXISTS `table1`; 

CREATE TABLE `table1` (
    `id` int(11) NOT NULL auto_increment, 
    `md` DATETIME, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB; 

DELIMITER // 
DROP PROCEDURE IF EXISTS autofill// 
CREATE PROCEDURE autofill() 
BEGIN 
    DECLARE i INT DEFAULT 0; 
    WHILE i < 50 DO 
     INSERT INTO table1 (md) VALUES (DATE_ADD(NOW(), INTERVAL FLOOR(RAND() * 1000) HOUR)); 
     SET i = i + 1; 
    END WHILE; 
END; 
// 
DELIMITER ; 

CALL autofill(); 

SELECT * FROM table1 ORDER BY DATE(md) DESC, RAND(); 

+----+---------------------+ 
| id | md     | 
+----+---------------------+ 
| 8 | 2010-08-16 13:46:04 | 
| 16 | 2010-08-15 19:46:05 | 
| 47 | 2010-08-15 18:46:06 | 
| 25 | 2010-08-15 15:46:05 | 
| 33 | 2010-08-15 09:46:05 | 
| 3 | 2010-08-14 20:46:04 | 
| 45 | 2010-08-13 03:46:06 | 
| 17 | 2010-08-12 13:46:05 | 
| 12 | 2010-08-12 06:46:05 | 
| 7 | 2010-08-12 15:46:04 | 
| 37 | 2010-08-12 14:46:05 | 
| 40 | 2010-08-10 05:46:06 | 
| 13 | 2010-08-09 10:46:05 | 
| 4 | 2010-08-09 00:46:04 | 
| 1 | 2010-08-06 12:46:04 | 
| 28 | 2010-08-02 23:46:05 | 
| 15 | 2010-08-02 10:46:05 | 
| 2 | 2010-08-02 01:46:04 | 
| 38 | 2010-07-31 13:46:06 | 
| 27 | 2010-07-31 04:46:05 | 
| 30 | 2010-07-31 03:46:05 | 
| 22 | 2010-07-31 08:46:05 | 
| 50 | 2010-07-30 05:46:06 | 
| 11 | 2010-07-28 13:46:05 | 
| 18 | 2010-07-28 21:46:05 | 
| 29 | 2010-07-27 23:46:05 | 
| 35 | 2010-07-27 11:46:05 | 
| 6 | 2010-07-26 20:46:04 | 
| 20 | 2010-07-25 03:46:05 | 
| 31 | 2010-07-23 07:46:05 | 
| 14 | 2010-07-23 23:46:05 | 
| 23 | 2010-07-23 10:46:05 | 
| 48 | 2010-07-23 17:46:06 | 
| 42 | 2010-07-21 03:46:06 | 
| 39 | 2010-07-20 05:46:06 | 
| 36 | 2010-07-18 05:46:05 | 
| 10 | 2010-07-17 01:46:05 | 
| 32 | 2010-07-16 06:46:05 | 
| 9 | 2010-07-16 15:46:04 | 
| 24 | 2010-07-16 10:46:05 | 
| 43 | 2010-07-16 09:46:06 | 
| 5 | 2010-07-14 01:46:04 | 
| 21 | 2010-07-14 08:46:05 | 
| 49 | 2010-07-13 07:46:06 | 
| 41 | 2010-07-13 15:46:06 | 
| 46 | 2010-07-12 04:46:06 | 
| 44 | 2010-07-11 16:46:06 | 
| 26 | 2010-07-10 14:46:05 | 
| 34 | 2010-07-09 16:46:05 | 
| 19 | 2010-07-07 01:46:05 | 
+----+---------------------+ 
+0

感谢迈克,我已经在这里尝试过,但有一个问题,rand()函数参数是一个刚才的种子编号,不影响日期(md)排序 – Rodrigo 2010-07-06 19:10:31

+0

第二个查询(GROUP BY一个)似乎不起作用。 – kennytm 2010-07-06 19:12:18

+0

这给了我一个想法,解决了这个问题 select * from(SELECT * FROM table1 ORDER BY rand())as x order by day(md); – Rodrigo 2010-07-06 19:14:32

5

很简单:

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