从datetime列中选择月份

时间:2016-08-19 22:04:33

标签: mysql datetime

我有一个表格,其日期时间列名为: completed_date (2016-05-27 04:08:53)。我想在忽略时间的情况下选择当前月份的所有数据。像这样:

  $query = mysqli_query($dbc, " select  * from eob_posting where
  completed_date=DATE(NOW())");

如何删除时间,以便 条款只考虑日期?

3 个答案:

答案 0 :(得分:3)

要获取当月,您应该使用YEAR()MONTH()

WHERE YEAR(completed_date) = YEAR(NOW()) AND MONTH(completed_date) = MONTH(NOW())

答案 1 :(得分:1)

甚至比其他答案还短: - )

请注意,日期时间基本上也是“2016-01-01 12:34:56”形式的字符串,因此您可以:

x

因此您的查询很容易表达为:

> SELECT LEFT(NOW(), 7) as YearAndMonth;
YearAndMonth
------------
2016-08

快乐狩猎!

答案 2 :(得分:0)

select  * from eob_posting 
where completed_date >= CAST(DATE_FORMAT(curdate() ,'%Y-%m-01') as DATE)