Sandesh
Sandesh

Reputation: 37

sql query for filtering data with where multiple criteria on same column of table

I have following Sample table. table having combination of name and key column unique records

ID   name      key          key_type 
-----------------------------------
118  ab         12          aw1 
119  fb         13          1Y2 
120  cb         14          qw3 
121  ab         15          4123
122  cs         12          23d2

select * from Sample where name ='ab' and key= '12' 

select * from Sample where name ='fb' and key= '13' 

how to write single query for both record ?

Upvotes: 0

Views: 92

Answers (2)

pala_
pala_

Reputation: 9010

Easiest way would be

select * 
  from Sample 
  where (name = 'ab' and `key` = '12') 
    or (name = 'fb' and `key` = '13')

Demo here: http://sqlfiddle.com/#!9/3eabc/3

Throw an index on (name, key) for good measure.

create index name_key on sample(name, `key`);

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

Easiest way would be union all

select * from Sample where name ='ab' and key= '12' 
union all
select * from Sample where name ='fb' and key= '13' 

Upvotes: 1

Related Questions