Ravi Kiran
Ravi Kiran

Reputation: 13

sql query to fetch multiple rows from a table matching in two columns

I have a table with 2 columns in which one column is unique. I want to fetch records from table as below, I want to query from my sql developer to fetch records from the table where transaction id = 195865487, 201263012 and transaction sequence is 1,4,5,6,7 for 195865487 and 2,3,4,5,6,7 for 201263012 .

transaction id  | transaction seq
----+-----------+-------------------                                              
195865487       |  1
201263012       |  1   
195865487       |  2
195865487       |  3
195865487       |  4
195865487       |  5
195865487       |  6
195865487       |  7
195865487       |  8 
201263012       |  2  
201263012       |  3  
201263012       |  4  
201263012       |  5  
201263012       |  6  
201263012       |  7 
201263012       |  8  
201263012       |  9

Upvotes: 0

Views: 1779

Answers (3)

Lisa Pearls
Lisa Pearls

Reputation: 1

This will give you the required results

SELECT * FROM table
WHERE (transaction_id=195865487 AND transaction_seq IN (1,4,5,6,7))
OR (transaction_id=201263012 AND transaction_seq IN (2,3,4,5,6,7))

Upvotes: 0

Ponder Stibbons
Ponder Stibbons

Reputation: 14858

Construct where condition like this:

select * from t 
  where (transaction_id = 195865487 and transaction_seq in (1,4,5,6,7))
     or (transaction_id = 201263012 and transaction_seq in (2,3,4,5,6,7))

demo

Upvotes: 1

Paul X
Paul X

Reputation: 379

Is this what you need?

SELECT * FROM some_table
WHERE transaction_id IN (195865487, 201263012)
AND (transaction_sequence IN (1,4,5,6,7)
OR transaction_sequence IN (2,3,4,5,6,7))

Upvotes: 0

Related Questions