select * from table where uname
in(‘aaa’,bbb’,‘ccc’,‘ddd’,‘eee’,ffff’’);
/* 拼接成in格式: IN('a','b','c') * @access public * @param mix $list 列表数组或字符串 * @return string */ function create_in($list = '') { if (empty($list)) { return " IN ('') "; } else { $str = joinString($list); return trim($str) == '' ? " IN ('') " : " IN (" . $str . ") "; } } /* * @name 拼接字符串,并且去掉重复项: 'a','b','c' * @access public * @param mix $list 列表数组或字符串 * @param bool $res_arr 是否返回数组 * @return string */ function joinString($list = '', $delimiter = ',', $res_arr = false) { if (!is_array($list)) { $list = explode($delimiter, $list); } $list = array_unique($list); $arr = array(); foreach ($list AS $v) { if (is_array($v)) { foreach ($v as $key => $val) { $arr[] = "'" . $val . "'"; } } elseif ($v !== '') { $arr[] = "'$v'"; } } if ($res_arr) { return $arr; } return count($arr) == 0 ? " " : join($delimiter, $arr); }
Done!