简体   繁体   English

MySQL:从表中选择日期为当前星期和当前月的数据

[英]MySQL: Select data from a table where the date falls in the current Week and current Month

I am creating a web app where if a user clicks on Link called "WEEK", the page shows up all the posts which were submitted in that week. 我正在创建一个Web应用程序,如果用户单击名为“ WEEK”的链接,该页面将显示该周提交的所有帖子。 There's also an option to view all the posts submitted this month. 还有一个选项可以查看本月提交的所有帖子。 There's a column in the posts table named post_date in which the date is stored in the format : YYYY-MM-DD. 帖子表中有一列名为post_date,其中日期以YYYY-MM-DD格式存储。

My Problem: I don't know how to generate the posts submitted this week and month. 我的问题:我不知道如何生成本周和本月提交的帖子。

I can successfully select the posts submitted on the current date like this: 我可以成功选择当前日期提交的帖子,如下所示:

$today = date("Y-m-d"); // This is the format of the DATE column named post_date in the posts table.
if($_GET['when'] == "today")
{
$when = $today;
}
else if ($_GET['when'] == "week")
{
$when = // I don't know what goes here if I want to generate posts submitted this week
}
else if ($_GET['when'] == "month")
{
$when = // I don't know what goes here if I want to generate posts submitted this month
}
// I have already connected to the database and selected the table.
$query = "SELECT * FROM posts WHERE post_date = '$when'";
$result = mysql_query($query);
// After this I Generate the Posts Using WHILE loop and mysql_fetch_array

If the $_GET['when']'s value in the URL is "today", the SCRIPT works fine and generates the posts submitted 'today'. 如果URL中的$ _GET ['when']值为“ today”,则SCRIPT可以正常工作并生成“ today”提交的帖子。 But I don't know how to select the current week and the current month. 但是我不知道如何选择当前星期和当前月份。 I have already done my homework: I have googled and also searched StackOverflow for matching questions but there were questions like data from Last Week and the code was straight forward and couldn't fit in my situation. 我已经完成了家庭作业:我已经搜索并搜索了StackOverflow以查找匹配的问题,但是存在诸如“上周”的数据之类的问题,并且代码很简单,无法满足我的情况。 For example, there was an option to select the 1st, 2nd, 3rd and 4th week by using WEEK($today,1) ( I don't know if it works or not, but I want to generate the posts submitted in the current week and month ). 例如,有一个选项可以通过使用WEEK($today,1)来选择第一,第二,第三和第四周(我不知道它是否有效,但是我想生成当前提交的帖子周和月)。

Thanks in advance. 提前致谢。

There are several MySQL functions that do things like this for you 几个MySQL函数可以为您做这样的事情

SELECT * FROM posts
WHERE
   WEEK(post_date) = WEEK(CURDATE())
   AND
   MONTH(post_date) = MONTH(CURDATE())

Well, by slightly modifying your code, you could realize the WEEK- and MONTH-Problem using SQL! 好吧,通过稍微修改代码,您可以使用SQL来实现WEEK和MONTH问题!

Regarding to: MySQL-Documentation , this might be the solution for you: 关于: MySQL文档 ,这可能是您的解决方案:

SELECT *
FROM   posts
WHERE  YEARWEEK(post_date) = YEARWEEK(NOW())

Second one may be a bit more complicated: 第二个可能更复杂:

SELECT *
FROM   posts
WHERE  YEAR(post_date) = YEAR(NOW())
AND    MONTH(post_date) = MONTH(NOW())

I hope, i could help you. 希望我能为您服务。

What about this? 那这个呢?

if($_GET['when'] == "today") {
    $query = "SELECT * FROM posts WHERE DATE(post_date) = CURDATE()";
} else if ($_GET['when'] == "week") {
    $query = "SELECT * FROM posts WHERE post_date >= DATE_SUB(NOW(), INTERVAL 1 WEEK)";
} else if ($_GET['when'] == "month") {
    $query = "SELECT * FROM posts WHERE post_date >= DATE_SUB(NOW(), INTERVAL 1 MONTH)";
}
$result = mysql_query($query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM