dream6120 2014-02-04 07:34
浏览 46
已采纳

显示按日期排序的随机结果

This is my database table structure:

-----------------------------------
|  id |     title    |    date    |
-----------------------------------
| 001 | my event OO1 | 04/02/2014 |
| 002 | my event OO2 | 04/02/2014 |
| 003 | my event OO3 | 04/02/2014 |
| 004 | my event OO4 | 05/02/2014 |
| 005 | my event OO5 | 05/02/2014 |
| 006 | my event OO6 | 05/02/2014 |
| 007 | my event OO7 | 05/02/2014 |
| 008 | my event OO8 | 06/02/2014 |
| 009 | my event OO9 | 06/02/2014 |
-----------------------------------

I succceed to display it like it :

04/02/2014
- my event OO1
- my event OO2
- my event OO3
05/02/2014
- my event OO4
- my event OO5
- my event OO6
- my event OO7
06/02/2014
- my event OO8
- my event OO9

But I want to display just 1 result (event) per date randomly like:

04/02/2014
- my event OO2
05/02/2014
- my event OO6
06/02/2014
- my event OO8

On my sql result, I have tried ORDER BY RAND() LIMIT 1 but of course I have not sorted by date so results are randomly ordered

So, how to get date sorted and have random results?

  • 写回答

3条回答 默认 最新

  • douwen7905 2014-02-04 07:41
    关注
    SELECT distinct t1.date, 
        (SELECT t2.ID, t2.title 
        FROM mytable as t2 
        WHERE t2.date = t1.date 
        ORDER BY RAND()
        LIMIT 1) AS event_title
    FROM mytable as t1 
    ORDER BY t1.date;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)