MySQL 选择从一年前到现在的所有记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5027687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:48:05  来源:igfitidea点击:

Selecting all records from one year ago till now

mysql

提问by Pieter888

I have a table filled with a lot of rows and I need to select all the rows that are less than a year old till now.

我有一张表格,里面有很多行,我需要选择到现在为止不到一年的所有行。

The table (called orders) has a DateTimecolumn named order_date, that's the field that determines when the order was placed.

表(称为orders)有一个DateTime名为的列order_date,这是确定何时下订单的字段。

How can I select all the records that have an order_datebetween now and a full year ago?

如何选择order_date从现在到一年前的所有记录?

回答by nos

select * 
from orders 
where order_date >= DATE_SUB(NOW(),INTERVAL 1 YEAR);

回答by Dimanenator I

SELECT * FROM order WHERE order_date >= curdate() - interval 1 year;

回答by zzapper

To first of month a year ago

到一年前的第一个月

SELECT DATE_SUB(DATE_FORMAT(CURRENT_DATE,'%Y-%m-01'),INTERVAL 1 YEAR);

回答by Sai Kalyan Kumar Akshinthala

I hope it helps you:

我希望它能帮助你:

select * 
from table 
where (order_date BETWEEN '2/15/2011 3:36:18 PM' AND '2/17/2011 9:00:00 PM')