简体   繁体   中英

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 ?

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' 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM