例如我按他的code_id,

抓出日期最接近現在的10筆,

要在這10筆中, 隨機取得5筆做為顯示.


1.抓出日期最接近現在的10筆
select code_id, date_create from tablename order by date_create DESC limit 10;

 code_id |     date_create    
---------+---------------------
    3037 | 2008-08-06 17:31:31
    3036 | 2008-08-05 12:17:42
    3035 | 2008-08-01 15:41:22
    3034 | 2008-08-01 15:41:01
    3033 | 2008-08-01 13:23:56
    3032 | 2008-08-01 13:08:01
    3031 | 2008-07-24 18:22:12
    3030 | 2008-07-22 16:38:47
    3029 | 2008-06-27 10:51:21
    3028 | 2008-06-24 14:42:58

2.隨機抓出5筆
select * from
    (select code_id, date_create from tablename order by date_create DESC limit 10) as data
order by random() limit 5;

 code_id |     date_create    
---------+---------------------
    3029 | 2008-06-27 10:51:21
    3030 | 2008-07-22 16:38:47
    3033 | 2008-08-01 13:23:56
    3032 | 2008-08-01 13:08:01
    3035 | 2008-08-01 15:41:22

arrow
arrow
    全站熱搜

    shelleyura 發表在 痞客邦 留言(0) 人氣()