這里分兩種情況來介紹
1、in 后面是記錄集,如:
select * from table where uname in(select uname from user);
2、in 后面是字符串,如:
select * from table where uname in('1','2','3');
注意:這里一定要將字符串用單引號''單個 標注起來;
也可以定義變量
$_str = '1,2,3';
select * from table where uname in($_str);
但嚴禁在in內用兩個單引號,會遇到sql語句錯誤,如下
select * from table where uname in('1,2,3');
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種情況”的形式
