SQL条件日期时间设置

时间:2019-01-08 08:22:51

标签: sqlite

控制台必须显示有关device_id数字1的信息,因为它没有更新超过1年,但我却获得了device_id 1和3

我确定,此问题在last_checking_date + 365 <′2019.01.09'处输入='termometr';但我无法解决它,如何为我的状况设置日期时间

CREATE TABLE workspaces ( 
workspace VARCHAR (255) NOT NULL, 
device_id INTEGER NOT NULL REFERENCES devices(id), 
setup_date DATETIME, 
last_checking_date DATETIME, 
checking_result VARCHAR (255) 
); 

CREATE TABLE devices ( 
id INTEGER PRIMARY KEY NOT NULL UNIQUE, 
type VARCHAR (255), 
realize_date DATETIME, 
accuracy FLOAT 
); 

INSERT INTO workspaces (workspace, device_id, setup_date, last_checking_date, checking_result)
VALUES ('plavka_metalla', ‘1’, '1993.01.11', '1993.01.12', 'Perfect');

INSERT INTO workspaces (workspace, device_id, setup_date, last_checking_date, checking_result)
VALUES ('him_zona', ‘2’, '1993.01.21', '2019.01.04', 'Perfect');

INSERT INTO workspaces (workspace, device_id, setup_date, last_checking_date, checking_result)
VALUES ('metallurgiya', ‘3’, '1994.02.24', '2019.01.04', 'Perfect');

INSERT INTO devices (id, type, realize_date, accuracy)
VALUES ('1', 'termometr', '1993.01.20', '0.91');

INSERT INTO devices (id, type, realize_date, accuracy)
VALUES ('2', 'air_checker', '2013.03.07', '0.94');

INSERT INTO devices (id, type, realize_date, accuracy)
VALUES ('3', 'termometr', '2013.09.27', '0.99');

SELECT workspace, device_id, type FROM workspaces INNER JOIN devices ON workspaces.device_id = devices.id WHERE last_checking_date + 365 < ’2019.01.09’ AND type = ‘termometr’;

1 个答案:

答案 0 :(得分:0)

首先,不要将日期文字存储为2019.01.09,而是将它们存储为2019-01-09。然后,尝试在查询中使用date函数:

SELECT
    workspace,
    device_id,
    type
FROM workspaces w
INNER JOIN devices d
    ON w.device_id = d.id
WHERE
    DATE(last_checking_date, '+1 years') < '2019.01.09' AND
    type = 'termometr';

请注意,在这里假设您是一年,我在这里特别添加1年而不是365天。每四年发生的every年有366天,而不是365天。