【问题标题】:SQLite filter records by part of column valueSQLite 按列值的一部分过滤记录
【发布时间】: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


    【解决方案1】:

    您可以使用函数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}
    );
    

    【讨论】:

    • 您的第一个查询没有对我在解决方案中添加的结果进行排序和分组。无论如何谢谢:)
    • 正确,因为我忘记了 WHERE 子句。我编辑了。
    • 你还是不见了group by
    • @Ahtisham 为什么要分组?您没有进行任何聚合。如果要过滤掉重复项,则应使用 DISTINCT。
    【解决方案2】:

    您可以轻松查询日期时间,请查看thisanswer relates to your problem

    SELECT
      InvoiceId,
      BillingAddress,
      date(InvoiceDate) InvoiceDate,
    Total
    FROM
      invoices
    WHERE
      InvoiceDate NOT BETWEEN '2009-01-03' AND '2013-12-01'
    ORDER BY
      InvoiceDate;
    

    另一个例子

    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(格式,时间字符串,修饰符,修饰符,...)

    更好地检查您是如何创建表格的,您的日期列必须是日期时间字段。 希望这可以帮助。提供任何反馈,以便我进一步帮助您。

    【讨论】:

      【解决方案3】:

      修改了@forpas 的解决方案以最适合问题:

      修改后的 Spinner 查询:

      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);
      

      【讨论】:

        猜你喜欢
        • 2022-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多