1、创建mysql测试表,create table test_city(cityid varchar(20))
2、插入测试数据,
insert into test_city values(1)
insert into test_city values('1,2,3')
insert into test_city values('1,2,3,4')
insert into test_city values(2)
insert into test_city values(3)
3、编写sql,查询cityid为3的记录,
select * from test_city where cityid in ('1','2','3','4','5','6')
4、编写sql,查询cityid为'1,2,3'的记录,
select * from test_city where cityid in ('1,2,3')
1.in 后面是记录集,如:
select * from table where uname in(select uname from user)2.in 后面是字符串,如:
select * from table where uname in('aaa',bbb','ccc','ddd','eee',ffff'')注意:这里一定要将字符串用单引号'' 标注起来;
3.in 后面是数组,用如下方法,请参考:
//$pieces是含数据的数组for($i=0$i<count($pieces)$i++){
$uname=$uname."'".$pieces[$i]."',"
}
$the_uname ="uname in(".$uname."'')"
select * from table where ".$the_uname."
备注:这种方法的原理其实很简单,二就是把数组编程上面“第2种情况”的形式。
Select * From UserWhere exists (Select * From UserTag Where User.Uid = UserTag.Uid and TagCode =3)
and exists (Select * From UserTag Where User.Uid = UserTag.Uid and TagCode=4)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)