用多个id,查询出一个表中多条数据,比如,id有 001 005 006,表中id从1-1000都有的话,那么 怎么用Mysql语句查询呢?
1、如果是mysql数据库中直接使用语句
select 列名 from 表名 where id in('001','005','006');
2、如果是在mybatis中
list中存放 001 005 006 这些
<select id="selectList" resultMap=“你的实体类的map”>
<if test="list!=null and list.size()>0">
select 列名 from 表名
where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</select>
select XX from XX where id in('001','005','006')
select * from 表名 where id in ('001','005','006')
IN 列表项不仅支持数字,也支持字符、时间日期等类型,并且可以将这些不同类型的数据项混合使用。
SELECT * FROM song_list_single WHERE id IN(1,2,3,4,5)
SELECT * FROM song_list_single WHERE id IN('1','2','3','4','5')
你的要求可以使用IN后面跟查询这样的方式
SELECT * FROM song_list_single WHERE id IN (SELECT id FROM song_list_single sl WHERE sl.id < 1000 )
闹着玩呢啊,这么多人回答的没有问题啊,为何不采纳
例如:根据id 范围查询数据
select * from dept
select * from dept where id between 1 and 999