从激活日期开始输出数据超过一年

时间:2017-02-20 08:17:33

标签: mysql sql

我有桌面客户:

CREATE TABLE clients (
id  INT AUTO_INCREMENT NOT NULL,
tariff VARCHAR(255) NOT NULL NULL,
validity VARCHAR(255) NOT NULL,
phoneNumber VARCHAR(255) NOT NULL ,
PRIMARY KEY(id)
) ENGINE=InnoDB CHARACTER SET=UTF8;

INSERT INTO clients (id,tariff,validity,phoneNumber) VALUES    (1,"tariff1","10-02-2015","380938474271");
INSERT INTO clients (id,tariff,validity,phoneNumber) VALUES (2,"tariff1","10-02-2016","380938474272");
INSERT INTO clients (id,tariff,validity,phoneNumber) VALUES (3,"tariff2","12-01-2017","380938474273");
INSERT INTO clients (id,tariff,validity,phoneNumber) VALUES (4,"tariff3","10-01-2017","380938474274");
INSERT INTO clients (id,tariff,validity,phoneNumber) VALUES (5,"tariff3","11-02-2017","380938474275");

我的字段有效期是激活日期。 如何从此表字段中选择激活日期超过一年的字段?例如,对于此表,它将是id为1和2的客户端。

1 个答案:

答案 0 :(得分:2)

您应该将varchar值转换为正确的日期,并检查是否为<现在1年以下

select * from clients
where str_to_date(validity , '%d-%m-%Y') < date_sub(now(), INTERVAL 1 YEAR )