[英]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
最简单的方法是
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 演示在这里: http : //sqlfiddle.com/#!9/3eabc/3
Throw an index on (name, key
) for good measure. 在(name,
key
)上添加一个索引以达到良好的效果。
create index name_key on sample(name, `key`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.