Page 1 of 1

how to make mysql display one of each value

Posted: Sat Oct 08, 2005 10:15 pm
by paqman
If an array has duplicate values (a, a, b, b, b, c, d, d) is there any way to make it only have one of each? (a, b, c, d). Thanks!

Posted: Sat Oct 08, 2005 10:17 pm
by feyd
using the GROUP BY aggregate system or SELECT DISTINCT .. depending on your query.

Posted: Sat Oct 08, 2005 11:32 pm
by paqman
okay distinct sort of worked... I have 3 entries to test it, and 2 are the same. I put it all in and I only get 2 results (it's working!) but they say: Resource id #4... Anyone ever heard of this before? Here's my code:

Code: Select all

$title_xbox = mysql_query("select distinct title from review where subsystem='xbox'");
$num_xbox=mysql_numrows($title_xbox);


echo "<h4>Xbox:</h4>";
while($r=mysql_fetch_array($title_xbox))
{
echo "<a href=\"reviews.php?cmd=view&title=$title_xbox\">$title_xbox</a><br>";
}
if ($num_xbox == 0) {
echo "No Xbox Reviews Found<br><br>";
}
Thanks for the help!

Posted: Sat Oct 08, 2005 11:36 pm
by feyd

Code: Select all

echo "<a href=\"reviews.php?cmd=view&title={$r['title']}\">{$r['title']}</a><br>";

solved

Posted: Sat Oct 08, 2005 11:43 pm
by paqman
thanks so much!

Posted: Mon Oct 10, 2005 12:36 am
by paqman
okay 1 more thing added on to that. Is there any way to get other values from the rows when using distinct? Or is there some way to do a second query to get the other values? here's the code I have so far:

Code: Select all

$exact_results = mysql_query("select distinct title from review where title='$title' and subsystem='$subsystem'");

while($r=mysql_fetch_array($exact_results))
{
$e_title = $r["title"];
$e_subsystem = $r["subsystem"];

echo "<tr><td><a href=\"index.php?id=submit&cmd=form&title=$e_title&subsystem=$e_subsystem\">$e_title</a></td><td>$e_subsystem</td></tr>";
}
What I would like is to be able to get the value 'subsystem' from the table. Thanks so much!

Posted: Mon Oct 10, 2005 1:04 am
by John Cartwright

Code: Select all

select distinct title, subsystem from review where subsystem='xbox'