为了账号安全,请及时绑定邮箱和手机立即绑定

Mysql多条件批量查询

Mysql多条件批量查询

冉冉说 2019-03-20 19:15:45
请教一个问题:mysql中,我查询语句select * from tableA where name='test' and age='18';然后我有一个name和age的list列表,分别通过这两个条件去查询,有什么比较方法可以查询;eg:select * from tableA where name='test' and age='18';select * from tableA where name='test1' and age='18';select * from tableA where name='test2' and age='20';select * from tableA where name='test2' and age='56';···其中查出来的数据对应Java的一个实体。其中List表的大小为500-3000,数据表的记录为每天8万左右
查看完整描述

5 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

table A 添加一列名称nameage 里面的值是 name和age的值合并 例如 test18
select * from tableA where nameage in ('test18','xxxxxx','xxxxxxx')

当然你不添加列也行,自己sql拼下也行


查看完整回答
反对 回复 2019-04-22
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

select a.* 

from tableA a

inner join list l on l.name=a.name and l.age=a.age

不行吗?


查看完整回答
反对 回复 2019-04-22
?
忽然笑

TA贡献1806条经验 获得超5个赞

数据量不大的时候,可以这样干。


SELECT * FROM tableA

WHERE (name, age) IN (

('test', 18),

('test1', 18),

('test2', 20),

('test2', 56))

但通过explain以上查询会发现,这种写法是没法用上索引的,所以查询效率很低。


查看完整回答
反对 回复 2019-04-22
  • 5 回答
  • 0 关注
  • 5206 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号