获取当前的周,月和年记录sql

时间:2017-08-31 07:45:54

标签: php mysql get range

您好我有一张表格,其列的值类似于 2017-08-21

请查看屏幕截图here

Now i need to get the values of current week, current month and current year.

请建议我如何实现这一目标。

感谢

2 个答案:

答案 0 :(得分:2)

本周:

SELECT * 
FROM  your_table 
WHERE YEARWEEK(`your_date`) = YEARWEEK(CURDATE())

当月

SELECT * 
FROM  your_table 
WHERE YEAR(`your_date`) = YEAR(CURDATE())
  AND MONTH(`your_date`) = MONTH(CURDATE())

今年

SELECT * 
FROM  your_table 
WHERE YEAR(`your_date`) = YEAR(CURDATE())

答案 1 :(得分:0)

选择当年的所有记录

Select *
FROM `table_name`
WHERE YEAR('hit_date') = YEAR(CURDATE());

选择当月的所有记录

Select *
FROM `table_name`
WHERE MONTH('hit_date') = MONTH(CURDATE())
AND YEAR('hit_date') = YEAR(CURDATE());

选择当前周的所有记录

Select *
FROM `table_name`
WHERE YEARWEEK('hit_date') = YEARWEEK(CURDATE());

请注意关于YEARWEEK,您可以指定您的周开始日取决于模式(第二个参数),请查看MySQL YEARWEEK() Function