Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple rows using id in SQLite

I am trying to query specific rows from my SQLite database using WHERE conditions on the IDs of the rows I want. However, the query is not returning any results, and it is causing my logger to throw an error stating that there is a syntax error.

Here is the query:

Select * From Equipment Where ID = 2 and ID = 3 and ID = 4 and ID = 7 and ID = 11 and ID = 34

And here is the syntax error from my log:

Aug 17 2017 23:12:23  [Err]: Err002 - Query on Equipment could not be prepared: near "=": syntax error at File: c:\users\geowil\documents\visual studio 2015\projects\ov_pocs\universearc_poc\datasystem.cpp  Line: 323.

So I sailed over to SQLFiddle to try and see if I missed something. It is displaying the same behavior. With and conditions, it returns no results - but running queries on single IDs or a range of IDs works.

Am I doing something wrong or is this a limitation of SQLite?

Update:

Had a brain wave. Using Where ID IN(1,3,4) works on SQLFiddle so now I want to repurpose my question to ask why this works, but my original query did not.

like image 593
Geowil Avatar asked Aug 18 '17 06:08

Geowil


People also ask

Which function is used to fetch multiple records SQLite?

To fetch all records we will use fetchall() method. where, cursor is an object of sqlite3 connection with database.

How do I SELECT specific data in SQLite?

To select data from an SQLite database, use the SELECT statement. When you use this statement, you specify which table/s to select data from, as well as the columns to return from the query. You can also provide extra criteria to further narrow down the data that is returned.

How do I SELECT multiple columns in SQLite?

To select multiple columns from a table, simply separate the column names with commas!

How do I limit the number of rows in SQLite?

Introduction to SQLite LIMIT clause You use the LIMIT clause to constrain the number of rows returned by the query. For example, a SELECT statement may return one million rows. However, if you just need the first 10 rows in the result set, you can add the LIMIT clause to the SELECT statement to retrieve 10 rows.


2 Answers

Use IN to select your multiple tuples.

Select * From Equipment Where ID IN (2, 3, 4, 7, 11, 34);

Every tuple will be selected if every ids existed in your SQLite or Database.


For your query, your logical equation will never select one of the tuple because an id won't have two or more values (ID=2 AND ID=3: if ID equal to 2 it won't be equal to 3). In your case you must use OR instead of AND.

Select * From Equipment Where ID=2 OR ID=3 OR ID=4 OR ID=7 OR ID=11 OR ID=34;

So that every tuple will be selected if every ids existed in your SQLite or Database.

like image 89
Sarith Nob Avatar answered Sep 24 '22 17:09

Sarith Nob


AND means that the conditions on both sides must be true. But for any single row, there can only be a single ID value.

To check for a match with any one of multiple values, replace AND with OR, or use IN.

like image 34
CL. Avatar answered Sep 20 '22 17:09

CL.