Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SQLite按部分列值筛选记录_Android_Sqlite_Datetime_Android Sqlite - Fatal编程技术网

Android SQLite按部分列值筛选记录

Android SQLite按部分列值筛选记录,android,sqlite,datetime,android-sqlite,Android,Sqlite,Datetime,Android Sqlite,背景: 以下是我的表格: 人员表: Id Name ------------- 1 user1 2 user2 订单表: Id Name Date YearMonth UserId ---------------------------------------------------------------------- 1 item1 2015-11

背景:

以下是我的表格:

人员表:

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
列是不必要的,我可以用同样的方法处理just date列,该列还提到订单的年份和月份,这就是我需要您帮助的地方


您能告诉我一种正确的方法,我可以使用日期列的一部分过滤上述查询,而不需要定义不必要的年-月列来过滤记录吗?

您可以轻松地查询日期-时间,请查看和

另一个例子

SELECT ord_num, ord_amount, ord_date, cust_code, agent_code 
FROM orders 
WHERE ord_date NOT BETWEEN '2008-08-02' AND '2008-30-02';
SQLite支持以下五种日期和时间函数:

  • 日期(时间字符串、修饰符、修饰符等)
  • 时间(时间字符串、修饰符、修饰符等)
  • 日期时间(时间字符串、修饰符、修饰符等)
  • julianday(时间字符串、修饰符、修饰符等)
  • strftime(格式、时间字符串、修饰符、修饰符等)
最好检查一下如何创建表,日期列必须是DateTime字段。
希望这有帮助。提供任何反馈,以便我可以进一步帮助您。

您可以使用函数
SUBSTR()
从列
Date
返回别名为
YearMonth
的列,以获取日期的前7个字符:

Cursor cursor = db.rawQuery(
    "SELECT SUBSTR(Date, 1, 7) YearMonth FROM orders WHERE UserId = ? ORDER BY YearMonth DESC",  
     new String[] {id + ""}
);
您的第二个问题是:

Cursor cursor = db.rawQuery(
    "SELECT * FROM orders WHERE UserId = ? AND SUBSTR(Date, 1, 7) = ?", 
    new String[] {id + "", selectedYearMonth}
);
对的解决方案进行了修改,使其最适合以下问题:

微调器的已修改查询:

Cursor cursor = db.query("orders", new String[] {"SUBSTR(orderDate, 1, 7) YearMonth"},
                        "userId = ?", new String[] {String.valueOf(personId)},
                        "YearMonth", null, "YearMonth DESC");    
已修改的订单获取查询:

Cursor cursor = db.query("orders",null,"userId = ? and SUBSTR(orderDate, 1, 7) = ?",
                         new String[] {personId+"", item}, null, null, null);

您的第一个查询不是对结果进行排序和分组,而是将其添加到解决方案中。无论如何谢谢:)正确,因为我忘了WHERE子句。我编辑了。你仍然缺少分组依据@Ahtisham为什么要分组依据?您没有进行任何聚合。如果要过滤掉重复项,则应使用DISTINCT。
Cursor cursor = db.query("orders",null,"userId = ? and SUBSTR(orderDate, 1, 7) = ?",
                         new String[] {personId+"", item}, null, null, null);