記事テーブルの記事があり、このフィールドは int(5) 型です。ここで、今日追加された記事の合計数を時間順に並べ替える必要があります。クエリ文は次のようになります:
SELECT * FROM `article` where date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d');
SELECT * FROM `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());
SELECT * FROM `article` where to_days(`add_time`) = to_days(now());
SELECT * FROM `article` where to_days(now()) – to_days(`add_time`) <= 1;
SELECT * FROM `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);
SELECT * FROM `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`);
SELECT * FROM `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
SELECT * FROM `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1;
mysql> select to_days('2010-11-22 14:39:51'); +--------------------------------+ | to_days('2010-11-22 14:39:51') | +--------------------------------+ | 734463 | +--------------------------------+ mysql> select to_days('2010-11-23 14:39:51'); +--------------------------------+ | to_days('2010-11-23 14:39:51') | +--------------------------------+ | 734464 | +--------------------------------+
mysql> select to_days('1997-10-07'), to_days('97-10-07'); -> 729669, 729669
mysql> select str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s'); +--------------------------------------------------------+ | str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s') | +--------------------------------------------------------+ | 2010-11-23 14:39:51 | +--------------------------------------------------------+