Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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
自动日期where子句-SQL_Sql - Fatal编程技术网

自动日期where子句-SQL

自动日期where子句-SQL,sql,Sql,我的数据库中有我可以这样过滤的日期: select * where a.y=2021 and a.m=2 and a.d=7 然而,如果我明天运行这个查询,我将不得不去那里手动更改 是否有一种方法可以自动执行此操作,就像我明天运行查询时,会得到d=8,第二天会得到d=9,依此类推 我尝试使用get date,但出现以下错误: SQL错误[6]:查询失败(#20210207_153809_06316_2g4as):行2:7:未注册函数“getdate” 我也不知道这是否是正确的解决方案。

我的数据库中有我可以这样过滤的日期:

select *
where    
a.y=2021 and a.m=2 and a.d=7
然而,如果我明天运行这个查询,我将不得不去那里手动更改

是否有一种方法可以自动执行此操作,就像我明天运行查询时,会得到d=8,第二天会得到d=9,依此类推

我尝试使用get date,但出现以下错误:

SQL错误[6]:查询失败(#20210207_153809_06316_2g4as):行2:7:未注册函数“getdate”


我也不知道这是否是正确的解决方案。有人知道如何解决这个问题吗?

你可以使用
现在
获取当前日期,并使用
获取部分日期

SELECT * 
FROM [TABLE]
WHERE a.y=YEAR(NOW()) and a.m=MONTH(NOW()) and a.d=DAY(NOW())

最好的解决方案是在数据中有一个
date
列。然后您可以使用:

where datecol = current_date
或者您的特定数据库用于当前日期的任何内容

否则,您必须将当前日期拆分为多个部分。在标准SQL中,如下所示:

where y = extract(year from current_date) and
      m = extract(month from current_date) and
      d = extract(day from current_date)
也就是说,众所周知,日期函数因数据库而异,因此确切的语法取决于您的数据库

例如,在SQL Server中编写此文件的常用方法是:

where y = year(getdate()) and
      m = month(getdate()) and
      d = day(getdate())

用正在使用的数据库标记您的问题。切换到数据库列的实际日期类型不是一个选项吗?为什么要在三个不同的列中存储日期值。您应该将其存储在数据类型为
date
的列中。是的,这正是我想要的,谢谢you@MarianaRodrigues很乐意帮忙,如果你觉得有用的话,你可以投票并接受答案