从MySQl表echo中过滤重复结果

时间:2012-03-14 19:50:22

标签: php mysql filter

首先,我是PHP-tarded并感谢任何帮助。所以,提前谢谢。

目标:回显查询结果并删除重复的“公司”结果。 我有一个包含以下列的表: id,安装程序,公司,地址,电话,代言,教区

“id”是主键,“installer”是个别挖掘设备操作员的名字和姓氏,“公司”是安装人员工作的公司,“教区”就像一个县......但我在路易斯安那州。所以教区。 我有一个下拉菜单,列出了所有的教区。选择教区后,所有安装程序及其关联公司都将显示在网页上。我的问题是,一家公司可能有多个安装程序,导致公司多次上市。我希望每家公司只上市一次。

这是我的PHP代码:


getuser.php


<?php
$q=$_GET["q"];

$con = mysql_connect('000.000.000.000', 'shanehward', '********');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("shanehward", $con);

$sql="SELECT * FROM installer_list WHERE Parish = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<thead>
<tr>
<th>Company</th>
<th>Address</th>
<th>City</th>
<th>Endorsements</th>
<th>Phone</th>
</tr>
</thead>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Company'] . "</td>";
  echo "<td>" . $row['Address'] . "</td>";
  echo "<td>" . $row['City'] . "</td>";
  echo "<td>" . $row['Endorsements'] . "</td>";
  echo "<td>" . $row['Phone'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

再次感谢您的任何意见。

2 个答案:

答案 0 :(得分:3)

您可以尝试此查询

SELECT DISTINCT company
FROM your_table
WHERE parish = selected_parish

或(如果你需要更复杂的东西)

SELECT company, COUNT(installer) as tot
FROM your_table
WHERE parish = selected_parish
GROUP BY company

答案 1 :(得分:0)

您可以执行GROUP BY以消除“重复”。

$sql="SELECT * FROM installer_list WHERE Parish = '".$q."' GROUP BY company";
相关问题