Search code examples
sqlsqlitedb-browser-sqlite

Mutiple PK's in where clause


In my table I have (LocID) which is my PK and I am trying to narrow it down to specific 4, however when I run the query below I get only one answer. What needs to be fixed in this query?

-- second we find out the name of the locations 

select name from location where locid = 524 and 512 and 505 and 506 ; 


Solution

  • The query you intended to write would be better expressed using WHERE IN:

    select name from location where locid = in (524, 512, 505, 506);
    

    What is actually happening in your current query is that the locid values on the RHS after the initial one (524) are being interpreted as literal true. So, your query is identical to this:

    select name from location where locid = 524 and true and true and true;
    

    This of course is just the same as:

    select name from location where locid = 524;
    

    That is, you will only get back records from matching 524.