tekartik / sqflite

SQLite flutter plugin
BSD 2-Clause "Simplified" License
2.87k stars 522 forks source link

How to do a SELECT query using WHERE IN? #15

Closed silverAndroid closed 6 years ago

silverAndroid commented 6 years ago

Does sqflite support using WHERE...IN? Because when I try

// Tried both IN (?) and IN ? with 1st argument of whereArgs as array and string
db.query('table', where: 'id IN (?)', whereArgs: [ids.join(', ')], orderBy: 'id ASC');

it returns 0 rows, which means it's most likely not forming the query properly (although I'm not sure how I can see the complete query being sent), and I'm sure it's not the DB missing the data or writing the query wrong because when I did a query on the DB myself, it returned the rows I was looking for.

alextekartik commented 6 years ago

yes IN is supported, I posted an example here https://github.com/tekartik/sqflite/blob/master/example/lib/exp_test_page.dart#L67-L77

Two solutions for you

silverAndroid commented 6 years ago

Thanks!

josebraz commented 3 years ago

If you query for strings: where: 'id IN (${strings.map((e) => "'$e'").join(', ')})'

marcosalberto1991 commented 2 years ago

tuve problemas en solucionar pero me llevo un tiempo sqflite: ^1.3.0+3

where: 'idDb IN (${user.map((e) => "?").join(', ')})', List<int> user = [ 2,3,5,9 ];

List<Map> maps = await dbClient.query(
      'programacionInspeccion',
      columns: ['*'],
      where: 'idDb IN (${user.map((e) => "?").join(', ')})',
      orderBy: 'fechaInicial ASC ',
      whereArgs: user,

    );