简体   繁体   English

SQL查询以选择添加日期的行在同一表的字段中的过期时间

[英]SQL query to select rows on date added where when they expired in a field in the same table

I have a table that has a date_added column and an expiry time, set in days. 我有一个表,其中有一个date_added列和一个以天为单位的到期时间。

I have tried the following: 我尝试了以下方法:

select * 
from `claims` 
where date_added(`date_added`, interval `expire_period` day) < date(now())

I have tried reformatting and still getting an error. 我尝试重新格式化,但仍然出现错误。

This is solved: 解决了:

SELECT * FROM dk . 选择* FROM dk claims WHERE DATE_ADD( date_added , interval expire_period day) < date(now()); claims WHERE DATE_ADD( date_added ,interval expire_period day)<date(now());

Try this: 尝试这个:

select * 
from claims 
where date_add(date_added, interval expire_period day) < date(now())

Reference of DATE_ADD DATE_ADD的参考

Your SQL syntax is wrong. 您的SQL语法错误。 There is no function date_added . 没有函数date_added What you need is date_add . 您需要的是date_add

SELECT * 
FROM claims 
WHERE DATE_ADD(date_added, INTERVAL expire_period DAY) < DATE(NOW());

Your error message will have told you something like: 您的错误消息将告诉您以下信息:

SQL ERROR (1064): You have an error in your SQL syntax; SQL错误(1064):您的SQL语法有错误; check the manual that correcsponds to your MySQL server version for the right syntax to use near ') < date(now())' at line 1. 检查与您的MySQL服务器版本对应的手册,以在第1行的')<date(now())'附近使用正确的语法。

So it did not know what to do with the closing bracket. 因此,它不知道如何使用右括号。 From there, you can track back to see where the open bracket is. 从那里,您可以追溯到打开的括号在哪里。 Next to date_added . date_added旁边。 And that is not a function. 那不是功能。

You can also rely on the database client. 您也可以依赖数据库客户端。 I'm using HeidiSQL on Windows, and it highlights all keywords (including functions). 我在Windows上使用HeidiSQL,它突出显示了所有关键字(包括函数)。 The date_added is not blue in your query. 查询中的date_added不是蓝色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM