sql = "select * from Table where 1=1" If A<>"" Then sql = sql & " and " & A If B<>"" Then sql = sql & " and " & B If C<>"" Then sql = sql & " and " & C If D<>"" Then sql = sql & " and " & D
能说一下 你的语言环境是用的什么呢? Asp Php Jsp 或者其他的呢 如果你用Jsp 的话 我有个好的办法给你解决
<?PHP $sqlWhere = 'id > 0';//这儿随你怎么写if($A != '')$sqlWhere.=" and A的表达式"; if($B != '')$sqlWhere.=" and B的表达式"; ?>
我一般是这样用 <?PHP $sqlWhere = 'id > 0';//这儿随你怎么写if($A != '')$sqlWhere.=" and A的表达式"; if($B != '')$sqlWhere.=" and B的表达式";$sql = "select * from Table where $sqlWhere"; ?>欢迎加入PHP爱好者群: 30169840
Php 的东东 我就不懂了 不过我的方法是 初始的 sql 变量 不需要有 Where 关键字就写 Select ... from table_name 定义一个 临时变量 tempSql 然后就判断 A,B,C,D if(A不等于空) tempSql = tempSql + "and name="+A---按照上面的方法 你把B , C , D 都判断了判断完成后构造的语句格式为 and name = A and age=B 最后 再 判断 tempSql 不为空的情况下 去掉tempSql 变量的前三个字符 也就是把 第一个 and 去掉 sql = sql + "where "+ tempSql
$sql = 'select * from Table where 1=1'; if($A!='') $sql = $sql . ' and ' . $A; if($B!='') $sql = $sql . ' and ' . $B; if($C!='') $sql = $sql . ' and ' . $C; if($D!='') $sql = $sql . ' and ' . $D; 其中$A之类的是xxx ='xxx'的形式;
If A<>"" Then sql = sql & " and " & A
If B<>"" Then sql = sql & " and " & B
If C<>"" Then sql = sql & " and " & C
If D<>"" Then sql = sql & " and " & D
Asp Php Jsp 或者其他的呢
如果你用Jsp 的话
我有个好的办法给你解决
$sqlWhere = 'id > 0';//这儿随你怎么写if($A != '')$sqlWhere.=" and A的表达式";
if($B != '')$sqlWhere.=" and B的表达式";
?>
<?PHP
$sqlWhere = 'id > 0';//这儿随你怎么写if($A != '')$sqlWhere.=" and A的表达式";
if($B != '')$sqlWhere.=" and B的表达式";$sql = "select * from Table where $sqlWhere";
?>欢迎加入PHP爱好者群: 30169840
不过我的方法是
初始的 sql 变量 不需要有 Where 关键字就写 Select ... from table_name
定义一个 临时变量 tempSql
然后就判断 A,B,C,D
if(A不等于空)
tempSql = tempSql + "and name="+A---按照上面的方法 你把B , C , D 都判断了判断完成后构造的语句格式为 and name = A and age=B
最后 再 判断 tempSql 不为空的情况下 去掉tempSql 变量的前三个字符 也就是把 第一个 and 去掉
sql = sql + "where "+ tempSql
if($A!='') $sql = $sql . ' and ' . $A;
if($B!='') $sql = $sql . ' and ' . $B;
if($C!='') $sql = $sql . ' and ' . $C;
if($D!='') $sql = $sql . ' and ' . $D;
其中$A之类的是xxx ='xxx'的形式;
LS的满足要求
if(!empty($A))
$aWhere[] = $A;
if(!emtpy($B))
$aWhere[] = $B;
if(!empty($C))
$aWhere[] = $C;
if(!empty($D))
$aWhere[] = $D;$sWhere = join(' and ', $aWhere);
$sSql = "select * from tbl where $sWhere order by id desc";
$link_symbol = "";
if($a)
{
$condition .= "$link_symbol $a";
$link_symbol = "and";
}
依次类推