Home > Back-end >  SQL multiple columns WHERE condition
SQL multiple columns WHERE condition

Time:01-11

I have table

Column A Column B Column C
1 a d
2 b e
3 c f

and very large .csv file (about 1M rows). It contains Columns A, B and C.

Column A Column B Column C
4 a d
5 b w
6 c f

I need to extract rows from table where (B = 'a' and C = 'd') or (B = 'b' and C = 'w') or (B = 'c' and C = 'f')

Result will be:

Column A Column B Column C
1 a d
3 c f

I've tried query like in description, but it's too large for request (1M rows)

CodePudding user response:

You are basically trying to join on the columns (b, c) manually. However, SQL knows how to do a join more efficiently.

Import the CSV file as a temporary table, analyze it, create a multi-column index on its (b, c), then do a join like this:

SELECT R.*
FROM realTable R
JOIN csvTable C
  ON R.b = C.b AND R.c = C.c;
  • Related