java - Mysql多条件批量查询
怪我咯
怪我咯 2017-04-18 09:35:24
[Java讨论组]

请教一个问题: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万左右

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(6)
ringa_lee

建立视图查询,这样效率会好点。至少比临时表效率稍快

巴扎黑

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

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

高洛峰

togeek的答案很帅
sql的效率可能要再考虑下

巴扎黑
select a.* 
from tableA a
inner join list l on l.name=a.name and l.age=a.age

不行吗?

黄舟

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

SELECT * FROM tableA
WHERE (name, age) IN (
('test', 18),
('test1', 18),
('test2', 20),
('test2', 56))

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

伊谢尔伦
select * from tableA where (name='test'and age='18')
or (name='test1'and age='18')
or (name='test2'and age='20')
...

创建临时表 join临时表中的2个字段

http://stackoverflow.com/ques...

http://www.zhihu.com/question...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号