0

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 ?

2
  • so whats the problem with the query you are trying? Commented Apr 30, 2015 at 10:03
  • WHERE (a AND b) OR (c AND d) should do the trick Commented Apr 30, 2015 at 10:36

2 Answers 2

1

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' 
Sign up to request clarification or add additional context in comments.

Comments

0

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`);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.