[英]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.