0

我需要一些用于快速多条件搜索的结构建议。所有表格列都有输入字段可供搜索。

如何处理空字段(不由用户填写/仅按给定信息搜索)?

谢谢 :)

4

2 回答 2

1

最好的方法是

WHERE
(col1=@col1 or @col1 is null) and
(col2=@col2 or @col2 is null) and
(col3=@col3 or @col3 is null) and
.
.

假设如果跳过该列进行搜索,则传递 null

于 2012-07-17T11:27:10.937 回答
0

您可以构建并附加 where 子句,如下所示

$query ="SELECT fields FROM tableName ";
$where ="";

 if(isset($_POST['field1']))
 {
   $field1=  mysql_real_escape_string($_POST['field1']);
   if($field1 != '')
   {
      $where . = "field1Name = $field1 AND ";
   }
 } 
   if(isset($_POST['field2']))
 {
   $field2=  mysql_real_escape_string($_POST['field2']);
   if($field2 != '')
   {
      $where . = "field2Name = $field2 AND ";
   }
 } 

 $where = rtrim($where, " AND ");
 $query . =$where; 
于 2012-07-17T11:32:36.337 回答