pablo1988 Posted April 10, 2012 Share Posted April 10, 2012 How can I stop duplication in the below code? Where do I implement the DISTINCT function? $sql="SELECT * FROM ((resource l inner join resource_skill ln on l.Resource_ID = ln.Resource_ID) inner join skill n on ln.Skill_ID = n.Skill_ID) WHERE First_Name LIKE '%" . $name . "%' OR Last_Name LIKE '%" . $name ."%' OR Skill_Name LIKE '%" . $name ."%'"; //-run the query against the mysql query function $result=mysql_query($sql); //-create while loop and loop through result set while($row=mysql_fetch_array($result)){ $First_Name =$row['First_Name']; $Last_Name=$row['Last_Name']; $Resource_ID=$row['Resource_ID']; //-display the result of the array echo "<ul>\n"; echo "<li>" . "<a href=\"a.php?id=$Resource_ID\">" .$First_Name . " " . $Last_Name . "</a></li>\n"; echo "</ul>"; } } Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted April 10, 2012 Share Posted April 10, 2012 have you tried SELECT DISTINCT * Quote Link to comment Share on other sites More sharing options...
pablo1988 Posted April 10, 2012 Author Share Posted April 10, 2012 Yes, that has no effect for some reason. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 10, 2012 Share Posted April 10, 2012 It's probably making mutliple matches because of the excessive use of LIKE. have you tried this? $sql = <<<SQL SELECT fields, that, you, want, and, need FROM resource INNER JOIN resource_skill ON (resource.Resource_ID = resource_skill.Resource_ID) INNER JOIN skill ON (resource_skill.Skill_ID = skill.Skill_ID) WHERE ( (First_Name LIKE '%$name%' OR Last_Name LIKE '%$name%' OR Skill_Name LIKE '%$name%') ) GROUP BY Skill_Name SQL; //-run the query against the mysql query function $result=mysql_query($sql); //-create while loop and loop through result set while($row=mysql_fetch_array($result)){ $First_Name =$row['First_Name']; $Last_Name=$row['Last_Name']; $Resource_ID=$row['Resource_ID']; //-display the result of the array echo "<ul>\n"; echo "<li>" . "<a href=\"a.php?id=$Resource_ID\">" .$First_Name . " " . $Last_Name . "</a></li>\n"; echo "</ul>"; } } Quote Link to comment Share on other sites More sharing options...
pablo1988 Posted April 10, 2012 Author Share Posted April 10, 2012 Thanks but I got errors with that. Is there not a way of adding the DISTINCT function into the code somewhere, or GROUP BY? DISTINCT worked when I was using SELECT DISTINCT First_Name etc but not with SELECT DISTINCT* Would hugely appreciate a solution to this! Thanks. Quote Link to comment Share on other sites More sharing options...
Sajesh Mohan Posted April 11, 2012 Share Posted April 11, 2012 please use GROUP BY Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 11, 2012 Share Posted April 11, 2012 Thanks but I got errors with that. Is there not a way of adding the DISTINCT function into the code somewhere, or GROUP BY? DISTINCT worked when I was using SELECT DISTINCT First_Name etc but not with SELECT DISTINCT* Would hugely appreciate a solution to this! Thanks. What errors? chalk up another reason not to use select * Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.