Jump to content

SELECT DISTINCT


pablo1988

Recommended Posts

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>";

    }

    }

Link to comment
Share on other sites

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>";
     }
     }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 *

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.