我有一张桌子,需要过滤那些比今天的日期少10天的日子,并且距离今天的日期不超过一个月。但是,日期的格式如下:
Last Thursday
A month ago
Two weeks ago
24 days ago
我正在使用SQLite3并且一直在尝试使用(当然还有所有其他代码)的语句
WHERE DATE('month', 1, available_from) > DATE('now')
但是没有运气。我该如何解决这个问题呢?我想这与写日期的方式有关,任何帮助都会很可爱谢谢你!
编辑: 我也考虑过使用案例,但是在如此情况下如何处理案例却没有任何线索。
答案 0 :(得分:0)
这是您如何使用CASES的基础: -
--DROP TABLE IF EXISTS funnydates;
--CREATE TABLE IF NOT EXISTS funnydates (datestore TEXT);
--INSERT INTO funnydates VALUES('Last Thursday');
--INSERT INTO funnydates VALUES('A month ago');
--INSERT INTO funnydates VALUES('Two weeks ago');
--INSERT INTO funnydates VALUES('24 days ago');
SELECT *, (datetimeinsecs - converteddate) AS difference FROM (
SELECT *, date('now'), strftime('%s','now') datetimeinsecs, strftime('%w','now') as currentdayofweek,
-- CREATE AND POPULATE SQL Day of the week number
-- Not really of use just showing use of multiple cases for multiple columns
CASE
WHEN datestore LIKE '%sunday' THEN 0
WHEN datestore LIKE '%MoNdAy' THEN 1
WHEN datestore LIKE '%TUESDAY%' THEN 2
WHEN datestore LIKE '%Wednesday%' THEN 3
WHEN datestore LIKE '%thursday%' THEN 4
WHEN datestore LIKE '%friday%' THEN 5
WHEN datestore LIKE '%saturday%' THEN 6
ELSE -1
END AS sqldayofweeknumber,
-- More along the lines of what could be done
CASE
WHEN datestore LIKE '%Last%' AND datestore LIKE '%Thursday%' THEN
strftime('%s','now') - ((7 - (4 - strftime('%w','now') )) * (60 * 60 * 24))
WHEN datestore LIKE '%Last%' AND datestore LIKE '%Friday%' THEN
strftime('%s','now') - ((7 - (5 - strftime('%w','now'))) * (60 *60 *24))
WHEN datestore LIKE 'two weeks ago' THEN
strftime('%s','now') - (2 * 7 * 24 * 60 * 60)
ELSE 'not converted'
END AS converteddate
FROM funnydates) WHERE (datetimeinsecs - converteddate) > (1 * 24 * 60 * 60)
sqldayofweeknumber
列不起作用。WHERE
子句仅包含超过1天(大约)的所需行(如图所示)。 10天后,它将是WHERE (datetimeinsecs - converteddate) > (10 * 24 * 60 * 60)
使用1天运行结果是: -
使用10天(WHERE (datetimeinsecs - converteddate) > (3 * 24 * 60 * 60)
)运行: -
请注意,这纯粹是示范的原则示例。整个解决方案显然会更复杂。
您可能希望参考: -