[英]sql query for filtering data with where multiple criteria on same column of table
我有以下示例表。 具有名称和键列唯一记录的组合的表
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'
如何为两个记录编写单个查询?
最简单的方法是union all
合并
select * from Sample where name ='ab' and key= '12'
union all
select * from Sample where name ='fb' and key= '13'
最简单的方法是
select *
from Sample
where (name = 'ab' and `key` = '12')
or (name = 'fb' and `key` = '13')
演示在这里: http : //sqlfiddle.com/#!9/3eabc/3
在(name, key
)上添加一个索引以达到良好的效果。
create index name_key on sample(name, `key`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.