如何在mysql表中选择当月的日期范围?

[英]How to select a date range of current month in mysql table?


I want to select current month rows in table

我想在表格中选择当前月份行

I have tried this

我试过这个

SELECT * FROM table WHERE YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE())

But it is not working.

但它没有用。

2 个解决方案

#1


3  

I'm not an expert on the MySQL engine, but my guess is that you'd be better off in general by comparing the column(s) to actual date values instead of using the date functions that you have. Once you wrap the column(s) in a date function it will make indexes on the column(s) useless.

我不是MySQL引擎的专家,但我的猜测是通过比较列和实际日期值而不是使用你拥有的日期函数,你会更好。一旦将列包装在日期函数中,它将使列上的索引无效。

I don't have a MySQL engine on this machine to test with, but here's some pseudocode:

我在这台机器上没有MySQL引擎来测试,但这里有一些伪代码:

SELECT
    <column list>
FROM
    My_Table
WHERE
    my_date >= <get 1st of current month>(CURDATE()) AND
    my_date <  <get 1st if next month>(CURDATE())

#2


0  

here is the simplest method which i have tried and works well, you want to select rows where date_field is in this month.

这是我尝试过的最简单的方法并且运行良好,你想要选择date_field在这个月里的行。

SELECT * FROM your_table WHERE date_field > (NOW() - INTERVAL 1 MONTH);

the above will return all records that the date_field is this month in mysql

以上将返回date_field在本月的所有记录在mysql中


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/07/01/4893a70654a8fb371e9c1153b89db4e9.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号