开发者

Sift out dubbels

开发者 https://www.devze.com 2023-02-06 17:35 出处:网络
Dear Sir/Mam iam trying to sort duplicates from a table. I want to check if a name has teh same streetname and then only show the first result of those, resulting in unique names ommitting the doubles

Dear Sir/Mam iam trying to sort duplicates from a table. I want to check if a name has teh same streetname and then only show the first result of those, resulting in unique names ommitting the doubles. I tried the distinct(name) and the group by statements but to no avail. Group by ended up limmiting my results

$klantquery = "SELECT name, ID, street, tel, email FROM  customers where name LIKE '%$search%' ORDER BY name ASC";    

This query开发者_开发问答 works but shows all records i need to sift out the extra ones? in order to show only unique results.

iam using mysql and php So now i used this query:

$klantquery = "SELECT DISTINCT naam,straat,email,huisnummer,plaats,date FROM ".$GLOBALS["klanten"]." where naam LIKE '%$klantsearch%' ORDER BY naam ASC";

Works like a charm but it omits the ID row in the results so its useless?? How do include the ID field in the results? Where is the logic?


Got it first i selected the records i needed with distinct then later added the IDs with a subquery, elegant not realy, works though.

$klantquery = "SELECT Distinct naam,straat,email,huisnummer,plaats,date FROM ".$GLOBALS["klanten"]." where naam LIKE '%$klantsearch%' ORDER BY naam ASC";                

while ($result=$klantpaging->result_assoc()) {

  $subquery = "SELECT ID,naam,straat FROM ".$GLOBALS["klanten"]." where naam='".addslashes($result['naam'])."' AND straat='".$result['straat']."'"; 
  $subresult = mysql_query($subquery) or die("Query failed : " . mysql_error());
  while ($subline = mysql_fetch_assoc($subresult)) {  
    $result[ID] = $subline[ID];
  }
  $klantrecords[] = $result;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消