Home > Backend Development > PHP Tutorial > 要筛选一个字段中符合条件的几种值应该如何查询

要筛选一个字段中符合条件的几种值应该如何查询

WBOY
Release: 2016-06-13 13:10:36
Original
1036 people have browsed it

要筛选一个字段中符合条件的几种值应该怎么查询?
是这样查询吗 select * from table where cateid=2 or cateid=4 or cateid=8 有没有其它方法..

我现在得到一个字符中cateid="2,4,8," 有什么简便的方法把符合条件的值给取出来呢,cateid的值长度不固定,有可能是cateid="2,4,8,23,44," 也有可能是cateid="2," 

谢谢........

------解决方案--------------------
select * from table where cateid in(2,4,8)
------解决方案--------------------
用in条件查询,但是条件的最后面的(,逗号)要去掉
------解决方案--------------------

SQL code

  select * from table where cate_id in (2,4,8)
<br><font color="#e78608">------解决方案--------------------</font><br>还是用or效率应该更高一些吧,用in写起来更容易一些<br>
Copy after login
PHP code

$a=explode(',',"2,4,8,23,44,");
$w = array();
foreach($a as $s){
  if(intval($s))
    $w[]='cate_id='.intval($s);
}
if(count($w))
  echo "select * from table where ".implode(' or ',$w); <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template