标题多重条件组合查询
日期:    作者:爱好者   来源:php之家
文章打印自:
访问文章完全地址:
头部广告
多重条件组合查询
平常我們在開發系統時,一定不會缺少查詢功能,然而往往算個條件是無法滿足用戶的要求的,這時我們就應該設置多重條件進行組合,讓用戶查詢時以更加方便地選擇查詢條件,迅速找到他們所滿足的記錄.
現在我們假設開發一個鐘表公司的客戶訂單系統,有客戶訂單編號,類別,名稱,數量,价格等資料,這里就不將資料一一列擊,.訂單表如下:
訂單編號(ID) 類別(TYPE) 名稱(NAME) 數量(QTY) 价格(PRICE)
001 机芯 机芯1 100 100
001 表殼 表殼1 1000 30
001 表帶 表帶1 500 60
002 机芯 机芯2 200 150
002 表殼 表殼2 500 40
002 表帶 表帶2 300 70
現在我們設想要建一個查詢系統,讓用戶即可根据訂單編號,也可根据類別,或者根据价格,或者其中的各自組合來實現快速查詢,代號如下:
下面此部分是顯示查詢條件的窗体
<html>
<?
$linkstr=mysql_connect("localhost","root","sa");
mysql_select_db("cx",$linkstr);
?>
<script language="JavaScript">
function variable()
{
if (document.search.select1.value!="1")
{
if (document.search.no.value=="")
{
window.alert("請輸入訂單號碼!");
return false;
}
else
{
if (document.search.type.value=="")
{
window.alert("請選擇配件類別!");
return false;
}
}
}
else
{
if (document.search.select2.value!="1")
{
if (document.search.price.value=="")
{
window.alert("請輸入价格!");
return false;
}
}
}
}
</script>
<br>
<center><font size="5"><b>多重條件組合查詢</b></font></center>
<br>
<body>
<form action="searchjg.php" method="POST" name="search" onsubmit="return variable(this.value)">
<table align="center" border="1" width="400">
<tr>
<td align="left" width="100">訂單編號</td>
<td align="left" width="300"><input type="text" name="no"></td>
</tr>
<tr>
<td align="left" width="200">
<select name="select1">
<option selected value="1">
<option value="2">或者
<option value="3">而且
</select>類別為
</td>
<td align="left" width="200">
<select name="type">
<option selected>
<?
$querystring="select distinct type from orders ";
$result=mysql_query($querystring,$linkstr);
while (list($type)=mysql_fetch_row($result))
{
echo "<option value="$type">".$type;
}
?>
</select>
</tr>
<tr>
<td>
<select name="select2">
<option selected value="1">
<option value="2">或者
<option value="3">而且
</select>价格位在
</td>
<td><select name="price">
<option selected>
<option value="1">50以下
<option value="2">50~200?
<option value="3">200以上
</select>的配件
</td>
</tr>
<table width="400" align="right">
<tr>
<td>
<input type="submit" name="submit" value="開始查詢">
</td>
</tr>
</table>
</table>
</form>
</body>
</html>

摘自:PHP中文用户
作者:Hafele
多重条件组合查询
接一來這部分是實現用戶提交之后的查詢結果,也是程序代碼的主体部份:
<?
$linkstr=mysql_connect("localhost","root","sa");
mysql_select_db("cx",$linkstr);
$showstr="查詢條件為:";
$querystring="select no,type,name,qty,price from orders";
switch($select1)
{
case 1:
if (!empty($no))
{
switch($select2)
{
case 1:
$querystring.=" where no=".$no;
$showstr.="訂單為".$no."所有數据";
break;
case 2:
switch($price)
{
case 1:
$querystring.=" where no=".$no." or price<50";
$showstr.="訂單為".$no."或者价格少于50元的所有數据";
break;
case 2:
$querystring.=" where no=".$no." or price between 50 and 200";
$showstr.="訂單為".$no."或者价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where no=".$no." or price>200";
$showstr.="訂單為".$no."或者价格大于200元間的所有數据";
break;
}
break;
case 3:
switch($price)
{
case 1:
$querystring.=" where no=".$no." and price<50";
$showstr.="訂單為".$no."并且价格少于50元的所有數据";
break;
case 2:
$querystring.=" where no=".$no." and price between 50 and 200";
$showstr.="訂單為".$no."并且价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where no=".$no." and price>200";
$showstr.="訂單為".$no."并且价格大于200元間的所有數据";
break;
}
break;
}
}
else
{
if (!empty($type))
{
switch($select2)
{
case 1:
$querystring.=" where type="".$type."" ";
$showstr.=" 類型為".$type."所有數据";
break;
case 2:
switch($price)
{
case 1:
$querystring.=" where type="".$type."" or price<50";
$showstr.="類型為".$type."或者价格少于50元的所有數据";
break;
case 2:
$querystring.=" where type="".$type."" or price between 50 and 200";
$showstr.="類型為".$type."或者价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where type="".$type."" or price>200";
$showstr.="類型為".$type."或者价格大于200元間的所有數据";
break;
}
break;
case 3:
switch($price)
{
case 1:
$querystring.=" where type="".$type."" and price<50";
$showstr.="類型為".$type."并且价格少于50元的所有數据";
break;
case 2:
$querystring.=" where type="".$type."" and price between 50 and 200";
$showstr.="類型為".$type."并且价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where type="".$type."" and price>200";
$showstr.="類型為".$type."并且价格大于200元間的所有數据";
break;
}
break;
}
}
else
{
switch($price)
{
case 1:
$querystring.=" where price<50";
$showstr.="价格少于50元的所有數据";
break;
case 2:
$querystring.=" where price between 50 and 200";
$showstr.="价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where price>200";
$showstr.="价格大于200元間的所有數据";
break;
}
}
}
break;
case 2:
switch($select2)
{
case 1:
$querystring.=" where no=".$no." or type="".$type.""";
$showstr.="訂單為".$no."或者類別為".$type."所有數据";
break;
case 2:
switch($price)
{
case 1:
$querystring.=" where no=".$no." or type="".$type."" or price<50 ";
$showstr.="訂單為".$no."或者類別為".$type."或者价格少于50元的所有數据";
break;
case 2:
$querystring.=" where no=".$no." or type="".$type."" or price between 50 and 200";
$showstr.="訂單為".$no."或者類別為".$type."或者价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where no=".$no."or type="".$type."" or price>200";
$showstr.="訂單為".$no."或者類別為".$type."或者价格大于200元間的所有數据";
break;
}
break;
case 3:
switch($price)
{
case 1:
$querystring.=" where no=".$no." or type="".$type."" and price<50 ";
$showstr.="訂單為".$no."或者類別為".$type."并且价格少于50元的所有數据";
break;
case 2:
$querystring.=" where no=".$no." or type="".$type."" and price between 50 and 200";
$showstr.="訂單為".$no."或者類別為".$type."并且价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where no=".$no."or type="".$type."" and price>200";
$showstr.="訂單為".$no."或者類別為".$type."并且价格大于200元間的所有數据";
break;
}
break;
}
break;
case 3:
switch($select2)
{
case 1:
$querystring.=" where no=".$no." and type="".$type.""";
$showstr.="訂單為".$no."并且類別為".$type."所有數据";
break;
case 2:
switch($price)
{
case 1:
$querystring.=" where no=".$no." and type="".$type."" or price<50 ";
$showstr.="訂單為".$no."并且類別為".$type."或者价格少于50元的所有數据";
break;
case 2:
$querystring.=" where no=".$no." and type="".$type."" or price between 50 and 200";
$showstr.="訂單為".$no."并且類別為".$type."或者价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where no=".$no."and type="".$type."" or price>200";
$showstr.="訂單為".$no."并且類別為".$type."或者价格大于200元間的所有數据";
break;
}
break;
case 3:
switch($price)
{
case 1:
$querystring.=" where no=".$no." and type="".$type."" and price<50 ";
$showstr.="訂單為".$no."并且類別為".$type."并且价格少于50元的所有數据";
break;
case 2:
$querystring.=" where no=".$no." and type="".$type."" and price between 50 and 200";
$showstr.="訂單為".$no."并且類別為".$type."并且价格介于50到200元間的所有數据";
break;
case 3:
$querystring.=" where no=".$no."and type="".$type."" and price>200";
$showstr.="訂單為".$no."并且類別為".$type."并且价格大于200元間的所有數据";
break;
}
break;
}
break;
}
$result=mysql_query($querystring,$linkstr);
echo "<p>$showstr:<p>
";
if (@mysql_num_rows($result)>0)
{
while (list($no,$type,$name,$qty,$price)=mysql_fetch_row($result))
{
echo "<table align="center">";
echo "<tr>";
echo "<td>訂單編號</td>";
echo "<td>$no</td>";
echo "</tr>";
echo "<tr>";
echo "<td>類別</td>";
echo "<td>$type</td>";
echo "</tr>";
echo "<tr>";
echo "<td>配件</td>";
echo "<td>$name</td>";
echo "</tr>";
echo "<tr>";
echo "<td>數量</td>";
echo "<td>$qty</td>";
echo "</tr>";
echo "<tr>";
echo "<td>价格</td>";
echo "<td>$price</td>";
echo "</tr>";
echo "</table>";
}
}
?>

摘自:PHP中文用户
作者:Hafele
责任编辑:semirock