限制下拉列表中相同值的输出

时间:2011-03-18 14:57:17

标签: php mysql drop-down-menu

$dept = $_POST['dept'];
$sql2 = "SELECT batch FROM $dept";
$result2 = mysql_query($sql2);
echo '<form method="post" id="try2" action="pillar.php">';
echo 'Select Batch: ';
echo '<select name="batch" id="batch">';
while($q = mysql_fetch_assoc($result2))
{
    $batch = $q['batch'];
    echo '<option>'.$batch.'</option>'; 
}
echo '</select><br><br>';
echo '<input type="submit">';

。如何限制使用select语句从数据库中检索的相同值输出数据?请帮忙!提前谢谢!

2 个答案:

答案 0 :(得分:0)

$sql2 = "SELECT batch FROM $dept GROUP BY `batch`";

GROUP BY子句会导致所有行按batch列进行分组,只会为每个batch值返回一行。

答案 1 :(得分:0)

为什么不对SQL查询进行分组?

"SELECT `batch` FROM $dept GROUP BY `batch`";