Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL从现在到去年的一个月内都会有记录_Php_Mysql_Sql_Date_Timestamp - Fatal编程技术网

Php MySQL从现在到去年的一个月内都会有记录

Php MySQL从现在到去年的一个月内都会有记录,php,mysql,sql,date,timestamp,Php,Mysql,Sql,Date,Timestamp,我正在尝试创建一个SQL查询,以选择时间戳小于NOW()但大于去年9月的记录,但我不想通过如下方式指定年份: date > '01/09/2014' 如果我今天运行查询,它将提供以下记录: 2014年9月至2015年5月(5月,因为这是创建此问题时的月份) 如果我明年运行它,它会给我 2015年9月至2016年5月(5月,因为这是创建此问题时的月份)您可以构建日期: where date >= str_to_date(concat(year(now()) - 1, '-09-01'

我正在尝试创建一个SQL查询,以选择时间戳小于NOW()但大于去年9月的记录,但我不想通过如下方式指定年份:

date > '01/09/2014'
如果我今天运行查询,它将提供以下记录:

2014年9月至2015年5月(5月,因为这是创建此问题时的月份)

如果我明年运行它,它会给我
2015年9月至2016年5月(5月,因为这是创建此问题时的月份)

您可以构建日期:

where date >= str_to_date(concat(year(now()) - 1, '-09-01'),
                          '%Y-%m-%d') and
      date < now()
where date>=str_to_date(concat(year(now())-1,'-09-01'),
“%Y-%m-%d”)和
日期<现在()
我猜你实际上想要一个稍微复杂一点的条件。如果在年底(比如12月)运行查询,您可能希望在当年的9月:

where date >= str_to_date(concat(year(now()) - (month(now) < 9), '-09-01'),
                          '%Y-%m-%d') and
      date < now()
where date>=str_to_date(concat(year(now())-(month(now)<9),“-09-01”),
“%Y-%m-%d”)和
日期<现在()

您可以构建日期:

where date >= str_to_date(concat(year(now()) - 1, '-09-01'),
                          '%Y-%m-%d') and
      date < now()
where date>=str_to_date(concat(year(now())-1,'-09-01'),
“%Y-%m-%d”)和
日期<现在()
我猜你实际上想要一个稍微复杂一点的条件。如果在年底(比如12月)运行查询,您可能希望在当年的9月:

where date >= str_to_date(concat(year(now()) - (month(now) < 9), '-09-01'),
                          '%Y-%m-%d') and
      date < now()
where date>=str_to_date(concat(year(now())-(month(now)<9),“-09-01”),
“%Y-%m-%d”)和
日期<现在()

如果您知道从哪个月开始需要数据,为什么不直接使用as
select*from table where date\u col在'2014-09-01'和curdate()之间
我会的,但这意味着明年它将无法正常工作,因为从2014年9月起它仍将获得记录,并且需要从2015年9月起。为什么不使用
intval(日期('Y'))-1)
确定该字符串?如果您知道从哪个月开始需要数据,为什么不直接使用as
select*from table,其中date_col在'2014-09-01'和curdate()之间
我会的,但这意味着明年它将无法正常工作,因为从2014年9月起,它仍将获得记录,并且需要从2015年9月起。为什么不使用
intval(date('Y'))-1)
来确定该字符串?这很完美,正是我想要的。非常感谢你,太完美了,正是我想要的。非常感谢你