从今天开始选择记录,本周,本月php mysql

时间:2011-03-13 23:16:35

标签: php mysql date select

我想这很简单,但无法弄清楚。我正在尝试制作几个页面 - 其中一个页面将包含今天,本周和本月从我的mysql数据库表中选择的结果。使用date('Y-m-d H:i:s');创建记录时输入日期。这是我到目前为止所做的:

日期日期>(日期 - (60 * 60 * 24))

 "SELECT * FROM jokes WHERE date>(date-(60*60*24)) ORDER BY score DESC"

周日期>(日期 - (60 * 60 * 24 * 7))

 "SELECT * FROM jokes WHERE date>(date-(60*60*24*7)) ORDER BY score DESC"

月(30天),日期>(日期 - (60 * 60 * 24 * 30))

 "SELECT * FROM jokes WHERE date>(date-(60*60*24*30)) ORDER BY score DESC"

任何想法都会非常感激。谢谢!

11 个答案:

答案 0 :(得分:117)

假设您的日期列是实际的MySQL日期列:

SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY score DESC;        
SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK) ORDER BY score DESC;
SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY score DESC;

答案 1 :(得分:70)

尝试使用日期和时间函数(MONTH(),YEAR(),DAY(),MySQL Manual

本周:

SELECT * FROM jokes WHERE WEEKOFYEAR(date)=WEEKOFYEAR(NOW());

上周:

SELECT * FROM jokes WHERE WEEKOFYEAR(date)=WEEKOFYEAR(NOW())-1;

答案 2 :(得分:37)

当月:

SELECT * FROM jokes WHERE YEAR(date) = YEAR(NOW()) AND MONTH(date)=MONTH(NOW());

本周:

SELECT * FROM jokes WHERE WEEKOFYEAR(date) = WEEKOFYEAR(NOW());

当天:

SELECT * FROM jokes WHERE YEAR(date) = YEAR(NOW()) AND MONTH(date) = MONTH(NOW()) AND DAY(date) = DAY(NOW());

这将只选择当前月份,真正的一周,实际上只选择今天: - )

答案 3 :(得分:9)

内森的答案会在过去的24小时,168小时和744小时内给你开玩笑,而不是本周本月的笑话。如果这就是你想要的,那很好,但我认为你可能正在寻找不同的东西。

使用他的代码,中午,你将从昨天中午开始,到今天中午结束。如果你真的想要今天的笑话,请尝试以下方法:

SELECT * FROM jokes WHERE date >= CURRENT_DATE() ORDER BY score DESC;  

你必须做一些与当前周,月等不同的事情,但你明白了。

答案 4 :(得分:4)

每个人似乎都把日期称为表格中的一列 我不认为这是好习惯。单词date可能只是某种编码语言中的关键字(也许是Oracle),所以请将列名日期更改为JDate 以下工作会更好:

SELECT * FROM jokes WHERE JDate >= CURRENT_DATE() ORDER BY JScore DESC;

因此我们有一个名为Jokes的表,其中包含JScore和JDate列。

答案 5 :(得分:3)

今天"更好的解决方案"是:

SELECT * FROM jokes WHERE DATE(date) = DATE(NOW())

答案 6 :(得分:2)

Nathan的答案非常接近,但它会返回一个浮动结果集。随着时间的推移,记录将浮出结果集。使用DATE()上的NOW()函数将从创建静态结果集的日期中删除时间元素。由于将date()函数应用于now()而不是实际的date列,因为将date()等函数应用于日期列会抑制MySql的使用能力,因此性能应该更高索引。

要保持结果集静态使用:

SELECT * FROM jokes WHERE date > DATE_SUB(DATE(NOW()), INTERVAL 1 DAY) 
ORDER BY score DESC;

SELECT * FROM jokes WHERE date > DATE_SUB(DATE(NOW()), INTERVAL 1 WEEK) 
ORDER BY score DESC;

SELECT * FROM jokes WHERE date > DATE_SUB(DATE(NOW()), INTERVAL 1 MONTH) 
ORDER BY score DESC;

答案 7 :(得分:0)

好吧,这个解决方案将帮助您只选择当前月份,当前周和仅今天

SELECT * FROM games WHERE games.published_gm = 1 AND YEAR(addedon_gm) = YEAR(NOW()) AND MONTH(addedon_gm) = MONTH(NOW()) AND DAY(addedon_gm) = DAY(NOW()) ORDER BY addedon_gm DESC;

For Weekly添加帖子:

WEEKOFYEAR(addedon_gm) = WEEKOFYEAR(NOW())

对于每月添加的帖子:

MONTH(addedon_gm) = MONTH(NOW())

对于每年添加的帖子:

YEAR(addedon_gm) = YEAR(NOW())

您将获得准确的结果,只显示今天添加的游戏,否则您可能会显示:"没有为今天找到的新游戏"。使用ShowIF记录集是空事务。

答案 8 :(得分:0)

我认为使用NOW()函数不正确以获得时差。因为每次计算过去24小时时都会NOW()函数。 您必须改为使用CURDATE()

    function your_function($time, $your_date) {
    if ($time == 'today') {
        $timeSQL = ' Date($your_date)= CURDATE()';
    }
    if ($time == 'week') {
        $timeSQL = ' YEARWEEK($your_date)= YEARWEEK(CURDATE())';
    }
    if ($time == 'month') {
        $timeSQL = ' Year($your_date)=Year(CURDATE()) AND Month(`your_date`)= Month(CURDATE())';
    }

    $Sql = "SELECT * FROM jokes WHERE ".$timeSQL
    return $Result = $this->db->query($Sql)->result_array();
}

答案 9 :(得分:0)

获取MySQL的上周数据。这对我来说甚至是全年的边界。

select * from orders_order where YEARWEEK(my_date_field)= YEARWEEK(DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK));

获取当前周的数据

select * from orders_order where YEARWEEK(date_sold)= YEARWEEK(CURRENT_DATE());

答案 10 :(得分:-1)

您可以使用单个查询执行相同的操作

SELECT sum(if(DATE(dDate)=DATE(CURRENT_TIMESTAMP),earning,null)) astodays,
       sum(if(YEARWEEK(dDate)=YEARWEEK(CURRENT_DATE),earning,null)) as weeks, 
       IF((MONTH(dDate) = MONTH(CURRENT_TIMESTAMP()) AND YEAR(dDate) = YEAR(CURRENT_TIMESTAMP())),sum(earning),0) AS months,
       IF(YEAR(dDate) = YEAR(CURRENT_TIMESTAMP()),sum(earning),0) AS years, 
       sum(fAdminFinalEarning) as total_earning FROM `earning`

希望这行得通。