sql过滤器表数据 [英] sql filter table data
本文介绍了sql过滤器表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含10列以上的表.我可以使用 sql
命令过滤数据,例如:
I have a table with more than 10 columns. I can filter data with sql
command like:
mysql_select_db("mydb", $con);
$sql="SELECT * FROM files WHERE PUB LIKE '".$PUB."' AND
ID LIKE '".$ID."' AND PG_NO LIKE '%".$PG_NO."%' AND
SECTION LIKE '".$SECTION."' AND EDITION LIKE '%".$EDITION."%' AND
COLOR LIKE '".$COLOR."' AND REVISION LIKE '".$REVISION."'";
问题是我必须填写搜索/过滤器查询的每个字段.我有时有时会使用所有字段和某些字段.
The problem is I have to fill every field of search/filter query. I want to sometimes use all the fields and some of the fields some other times.
有什么方法可以仅使用1个或2个过滤条件进行过滤,其余的过滤器留空以显示所有对应的数据?
Is there any way that I can filter with only 1 or 2 filter criteria and rest filters are left blank to show all corresponding data?
推荐答案
在调用SQL语句之前将其添加到
Add this to before you call your SQL statement
if(isset($PUB)){}
else{
$PUB = "%";
}
if(isset($ID)){}
else{
$ID = "%";
}
if(isset($SECTION)){}
else{
$SECTION = "%";
}
if(isset($EDITION)){}
else{
$EDITION = "%";
}
if(isset($COLOR)){}
else{
$COLOR = "%";
}
if(isset($REVISION)){}
else{
$REVISION = "%";
}
如果没有选择任何选项,我们本质上就是在给所有标准通配符加符号.
What we are essentially doing is giving all the criteria wildcard signs if no option was chosen.
这篇关于sql过滤器表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文