【发布时间】:2020-11-13 22:24:36
【问题描述】:
背景:
以下是我拥有的表格:
人员表:
Id Name
-------------
1 user1
2 user2
订单表:
Id Name Date YearMonth UserId
----------------------------------------------------------------------
1 item1 2015-11-01 02:34:46 2015-11 2
2 item2 2018-09-06 01:04:16 2018-09 1
3 item3 2018-09-23 04:44:21 2018-09 1
4 item4 2018-09-02 11:10:08 2018-09 2
5 item5 2019-11-01 02:54:02 2019-11 1
在 UI 方面,我为每个用户定义了一个微调器,其中填充了订单表的 YearMonth 列。
例如对于user1,微调器将填充:
- 2019-11
- 2018-09
我使用这个查询来填充微调器:
Cursor cursor = db.query("orders",new String[] {"YearMonth"}, "UserId = "+id,
null, "YearMonth", null, "YearMonth DESC");
// id is a local variable which stores the id of a particular user
每当我从微调器中选择任何 YearMonth 时,该查询都会返回该年和月带来的所有订单:
Cursor cursor = db.query("orders",null,"UserId = ? and YearMonth = ?",
new String[] {id+"", selectedYearMonth}, null, null, null);
// selectedYearMonth is the value of spinner which is currently selected.
问题:
如果您仔细注意到订单表中的 YearMonth 列是不必要的,我可以只使用日期列做同样的事情,其中还提到了订单的年份和月份,这就是我需要您帮助的地方。
你能告诉我一个正确的方法,我可以用部分日期列过滤上述查询,而无需定义不必要的 YearMonth 列来过滤记录吗?
【问题讨论】:
标签: java android sqlite datetime android-sqlite