I have created table with Employee, and I have columns such as Name, WorkingDays (datatype is date). I want to write a query that will output something like "select Name from Employee where WorkingDay = 'monday', 'wednesday' and 'friday' " i.e..I want to make a roaster for the workers. I am using sqlite and QT. thanks in advance.
问问题
418 次
1 回答
2
根据日期和时间函数文档,您可以通过strftime
这种方式使用函数:
SELECT Name FROM Employee WHERE strftime('%w',WorkingDay) IN ('1','3','5');
('1','3','5')
分别在哪里('Monday', 'Wednesday', 'Friday')
。
题外话:
工作日(数据类型为日期)
SQLite 中没有这样的东西。来自SQLite 版本 3文档中的数据类型:
1.2 日期和时间数据类型
SQLite 没有为存储日期和/或时间预留存储类。相反,SQLite 的内置日期和时间函数能够将日期和时间存储为 TEXT、REAL 或 INTEGER 值:
- TEXT 作为 ISO8601 字符串(“YYYY-MM-DD HH:MM:SS.SSS”)。
- REAL 作为儒略日数字,根据预测的公历,自公元前 4714 年 11 月 24 日格林威治中午以来的天数。
- INTEGER 作为 Unix 时间,自 1970-01-01 00:00:00 UTC 以来的秒数。
应用程序可以选择以任何这些格式存储日期和时间,并使用内置的日期和时间函数在格式之间自由转换。
更新
例如,如果今天是星期五,那么这个名字会自动出现在今天工作的名字上。可能吗?
是的。只需要strftime()
正确使用功能。我给你举个小例子:
CREATE TABLE Employee(
id INTEGER PRIMARY KEY,
name VARCHAR
);
CREATE TABLE WorkingDays(
id INTEGER PRIMARY KEY,
employee INTEGER,
dayNumber INTEGER,
FOREIGN KEY(employee) REFERENCES Employee(id),
UNIQUE(employee,dayNumer)
);
INSERT INTO Employee(name) VALUES('Max');
INSERT INTO Employee(name) VALUES('dic19');
INSERT INTO WorkingDays(employee,dayNumber) VALUES(1,1); --Max, Monday
INSERT INTO WorkingDays(employee,dayNumber) VALUES(1,2); --Max, Tuesday
INSERT INTO WorkingDays(employee,dayNumber) VALUES(1,3); --Max, Wednesday
INSERT INTO WorkingDays(employee,dayNumber) VALUES(2,4); --dic19, Thursday
INSERT INTO WorkingDays(employee,dayNumber) VALUES(2,5); --dic19, Friday
INSERT INTO WorkingDays(employee,dayNumber) VALUES(2,6); --dic19, Saturday
SELECT e.name
FROM WorkingDays w
LEFT JOIN Employee e ON w.employee = e.id
WHERE cast(w.dayNumber as text) = strftime('%w','now');
-- Right now it should return 'dic19' because Today is Friday/Saturday (depends on time zone)
SELECT e.name
FROM WorkingDays w
LEFT JOIN Employee e ON w.employee = e.id
WHERE cast(w.dayNumber as text) = strftime('%w','2013-11-11');
-- Should return 'Max' because '2013-11-11' was Monday
于 2013-11-14T22:07:23.730 回答