Mysql IN语句查询

时间:2021-05-01 19:18:55

语法:

WHERE column IN (value1,value2,...)
WHERE column NOT IN (value1,value2,...)

1、in 后面是记录集,如:

select  *  from  table  where   uname  in(select  uname  from  user); 

例子:

SELECT * FROM article WHERE uid IN(SELECT uid FROM user WHERE status=0)
在这个 SQL 例子里,我们实现了查出所有状态为 0 的用户(可能是被禁止)的所有文章。首先通过一个查询得到所有所有 status=0 的用户:

2、in 后面是字符串,如:

select  *  from  table  where   uname  in('aaa',bbb','ccc','ddd','eee',ffff''); 

注意:这里一定要将字符串用单引号'' 标注起来;

3、in 后面是数组,用如下方法,请参考:

$list 是 ' 1 , 2 , 3 , 4 , 5 '

 foreach (explode(',', $list) as $value) {
$rules=$rules."'".$value."',";
}
$arr = $this->db->selectAll('slim_auth_rule'," id IN (".$rules."'')",'id,title');