Questions tagged [where-in]

An SQL-standard condition of the form WHERE SOME_COLUMN IN (1,2,3) or using a subquery to create the list, eg WHERE SOME_COLUMN IN (SELECT X FROM MYTABLE WHERE Y)

For example:

SELECT *
FROM MY_TABLE
WHERE MY_COLUMN IN (1,2,3,5,8,13)

or commonly using a subquery to generate the list:

SELECT *
FROM MY_TABLE
WHERE MY_COLUMN IN (
    SELECT SOME_COLUMN FROM SOME_TABLE WHERE <some condition>
)

The subquery version is a source on many performance problems, because most optimizers do not optimize this properly.

Most IN (subquery) queries can be rewritten to use a join that does perform well.

497 questions
5
votes
3 answers

Cross-database prepared statement binding (like and where in) in Golang

After reading many tutorials, I found that there are many ways to bind arguments on prepared statement in Go, some of them SELECT * FROM bla WHERE x = ?col1 AND y = ?col2 SELECT * FROM bla WHERE x = ? AND y = ? SELECT * FROM bla WHERE x = :col1 AND…
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
5
votes
1 answer

How to write nested query in ruby on rails?

i want write a query for the following using where method SELECT * FROM videos WHERE 'privacy' = 'public' OR (privacy = 'private' AND id IN (SELECT vid FROM vid_ads WHERE 'aid'=#{current_id})) I tried…
nbbk
  • 1,102
  • 2
  • 14
  • 32
5
votes
2 answers

How to use WHERE IN in Rails 3 project using MySQL 2?

I am quite to new to Rails and have marginal experience with SQL-type languages. I am working on a Rails 3 project using a MySQL2 I have here a generic SQL statement that I want to work on BOTH of our databases. Is there any way to do this through…
user1971506
  • 2,267
  • 4
  • 19
  • 19
4
votes
3 answers

Why is MySQL not using indexes with composite WHERE IN?

I am trying to get several records by composite index from a table having PRIMARY KEY (a, b) SELECT * FROM table WHERE (a, b) IN ((1,2), (2,4), (1,3)) The problem is, that MySQL is not using index, even if I FORCE INDEX (PRIMARY). EXPLAIN SELECT…
vearutop
  • 3,924
  • 24
  • 41
4
votes
3 answers

select rows where not in multiple columns mysql

I have a following result set: request_id | p_id 66 | 10 66 | 10 66 | 10 66 | 22 66 | 22 76 | 23 76 | 24 I am trying to select rows that excludes records with certain…
Azima
  • 3,835
  • 15
  • 49
  • 95
4
votes
2 answers

Is this the proper way to handle an ordered array with Doctrine2's WHERE IN expression?

Using Zend Lucene Search, I am returning a list of relevance-ordered IDs that map to blog records that I will fetch from the database. Is this the proper way of handling an array with Doctrine2's WHERE IN expression: $dql = "SELECT b FROM BlogPost…
Stephen Watkins
  • 25,047
  • 15
  • 66
  • 100
4
votes
1 answer

SQLite Order by WHERE IN clause

I'm using SQLite and I'm providing a list of id values to retrieve. However, the order of those id values is important and I want to retrieve the records in the same order. For example, SELECT * FROM todos WHERE todos.id in ( 1, 3, 2, 4 )…
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
4
votes
2 answers

Laravel: whereIn with variable

I'm trying to collect all the records that belong to the sections that happen to them in the variable $sections. But only those from section 1 pick me up. Any suggestions? $sections = '1,2,3'; $data = News::where('active', '1') …
nature
  • 307
  • 5
  • 23
4
votes
3 answers

Using "in" in where condition with case

I have a sql table which has one column "type". It has values A,B,C,D. I'm writing a stored procedure in which type is the in parameter @type. @type can has either value 'A' or 'A,B,C' or 'ALL' based on user selection on screen. Which means user…
CrazyCoder
  • 2,194
  • 10
  • 44
  • 91
4
votes
3 answers

Passing multiple values for one SQL parameter

I have a CheckBoxList where users can select multiple items from the list. I then need to be able to pass these values to my Stored Procedure so they can be used in a WHERE condition like: WHERE ID IN (1,2,3) I tried doing this so that its a…
Curtis
  • 101,612
  • 66
  • 270
  • 352
4
votes
1 answer

Dapper WHERE IN string statement with Postgres

I've seen Dapper WHERE IN statement with ODBC But I'm not sure Dapper supports WHERE IN ("String1", "String2") syntax for Postgres. Is this supported? I tried digging through the code but I really don't have the time at the moment. So far I've only…
Jack
  • 9,156
  • 4
  • 50
  • 75
4
votes
1 answer

Python/pg8000 WHERE IN statement

What is the correct method to have the tuple (names) be available via %s in the SQL statement? names = ('David', 'Isaac') sql = 'SELECT * from names WHERE name IN %s' cur.execute(sql,(names,)) The response in…
Tsachi
  • 57
  • 6
4
votes
2 answers

why sql with 'exists' run slower than 'in' using MySQL

I am a newbie in MySQL optimization, i found a amazing thing:sql with 'exists' run slower than using 'in' !!! following is my DDL: mysql> `show create table order\G`; *************************** 1. row *************************** Table:…
lost_in
  • 75
  • 2
  • 7
4
votes
4 answers

MYSQL query WHERE IN vs OR

I have developed a system using an OR query: SELECT * FROM tableA JOIN tableB ON (idA = idB) WHERE idA = 1 OR idA = 2 OR idA = 3 OR idA = 4 OR idA = 5 ...... OR idA=100 Compare with query IN: SELECT * FROM…
hendra
  • 101
  • 4
  • 15
4
votes
4 answers

Does SQL performance degrade as the number elements in an "IN" clause increases?

I have a query like this, SELECT Name FROM Customers WHERE Id IN (1,4,3,6,7) There might be millions of customers in the DataBase. Will there be an efficiency problem with this query ? When the number of Ids inside IN statement are more ? If so,…
Novice
  • 2,447
  • 3
  • 27
  • 33
1 2
3
33 34