选择mysql表中的所有列,但不显示重复的结果,SELECT DISTINCT id

时间:2014-05-09 08:01:30

标签: php mysql duplicates

我正在尝试选择*我的表中的列并通过使用mysql查询来获取这些列,我得到一行的重复结果,所以我想不显示来自mysql的重复结果。我试图使用SELECT DISTINCT'id'

来做到这一点

理论上,这不应该第二次拉/作为重复结果,因为我告诉它只选择每个id一次。

这不起作用,我仍然得到重复的结果,请有人告诉我我哪里出错了吗?

$data3 = mysql_query("SELECT DISTINCT id, company_name, company_reg_number, date   
 FROM supplier_session WHERE status != 'Failed'
 ORDER BY date ASC") 
 or die(mysql_error()); 
 while($row = mysql_fetch_array( $data3 )) { 
 $days = $row['date'];
 $when = $days*0; 
 $str = $row['date'];
 $str2 = substr($str, 0); // "quick brown fox jumps over the lazy dog."
 if ($when <= 31){
 echo "<tr><td style=\"width:100px;\"><p>".$row['id'] . "</p></td>"; 
 echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>"; 
 echo "<td style=\"width:150px;\"><p>".$row['company_reg_number'] . "</p></td>";
 echo "<td>"; echo date('d/m/Y',strtotime($row['date'])); echo "</td>";
 if ($days >= 8) {
 echo "<td style=\"width:200px;\"><p>";
 print ($str2 -7);
 echo " days overdue</td>";      
 }
 elseif ($when <= 7){
 echo "<td style=\"width:200px;\"><p>Completed {$str2} days ago</td>";
 }}
 $q2 = "SELECT DISTINCT id, company_name, company_reg_number, date FROM supplier_session WHERE status != 'Failed' " or die (mysql_error());
 $result2 = mysql_query($q2);
 $count2 = mysql_num_rows($result2);
 echo "<td>";
 echo "Pending</td>";
 echo "<tr>";
 }

我的表格如下:

id  |  session_number  |  user_IP  |  date  |  company_reg_number  |  company_name |  form1_completed  |  form2_completed  |  form3_completed  |  form4_completed  |  completed_date  |  status

1 个答案:

答案 0 :(得分:1)

这里只是一个黑暗的镜头,但是,尝试按ID分组?

SELECT id, company_name, company_reg_number, date   
FROM supplier_session WHERE status != 'Failed' GROUP BY id