我的项目中有一个 sqlite 数据库,我有 3 个year
和month
字段day
。我想从我的表中获取上周或上个月的信息,并使用光标适配器将其显示在 listView 中。我不知道该怎么做。我的表不是很大,我的记录也按日期排序,所以迭代可以作为答案。请以任何方式帮助我。
问问题
2004 次
2 回答
0
SELECT * FROM myTable WHERE DATE(timeStamp) >= DATE('now', 'weekday 0', '-7 days');
于 2013-08-06T10:39:27.750 回答
0
如果桌子是这样的
CREATE TABLE t(some_data, year, month, day);
INSERT INTO t(some_data, year, month, day)
SELECT ABS(RANDOM()%10000), 2013, 8, 6 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 8, 4 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 8, 01 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 07, 31 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 7, 30 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 7, 25 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 07, 24 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 7, 23 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 7, 20 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 7, 10 UNION ALL
SELECT ABS(RANDOM()%10000), 2013, 7, 9
;
这是一个可能的解决方案
SELECT some_data, year
|| '-' ||
CASE WHEN LENGTH(month) = 2 THEN month
WHEN LENGTH(month) = 1 THEN 0 || month
END
|| '-' ||
CASE WHEN LENGTH(day) = 2 THEN day
WHEN LENGTH(day) = 1 THEN 0 || day
END as d
FROM t
WHERE DATE(d) >= DATE('now', '-7 days')
ORDER BY d;
some_data d
---------- ----------
7455 2013-07-30
6123 2013-07-31
6903 2013-08-01
9332 2013-08-04
1895 2013-08-06
于 2013-08-06T15:21:46.680 回答