首页 > 解决方案 > 如何使用 whereArgs 在 SQLite Flutter 中选择 DISTINCT 条目?

问题描述

我想通过给出一些条件(例如返回 ID = x 的所有不同记录(其中 x 将由用户获取))从 SQLITE 颤振中的表中返回不同的记录。

我怎样才能做到这一点?

标签: sqliteflutterdistinct

解决方案


使用SQFlite 包,您可以使用 aquery或 arawQuery来执行此操作,在这两种情况下,您都需要传递where argumentsas an array,并且distinct operator您可以按如下方式执行:

  /// [GET] Get all the data by date using a query
  getData(String date) async {
    final db = await database;
    final response = await db.query(
      "TableExample",
      distinct: true,
      orderBy: 'id',
      where: 'date = ?',
      whereArgs: [date],
    );
    //...
  }

  /// [GET] Get all the data by date using a rawQuery
  getDailyPollResponsesPerRangeDate(String date) async {
    final db = await database;
    final response = await db.rawQuery(
      "SELECT DISTINCT TableExample.id, TableExample.date "
      "FROM TableExample "
      "WHERE TableExample.date == ? "
      "ORDER BY TableExample.date DESC",
      [date],
    );
    //...
  }

推荐阅读