我有以下Sample表。 具有名称和键列唯一记录组合的表
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'
如何为两个记录编写单个查询?
答案 0 :(得分:1)
最简单的方式是union all
select * from Sample where name ='ab' and key= '12'
union all
select * from Sample where name ='fb' and key= '13'
答案 1 :(得分:0)
最简单的方法是
select *
from Sample
where (name = 'ab' and `key` = '12')
or (name = 'fb' and `key` = '13')
在这里演示:http://sqlfiddle.com/#!9/3eabc/3
在(名称,key
)上投掷索引以获得良好的衡量标准。
create index name_key on sample(name, `key`);