Ian Gooch Ian Gooch - 6 months ago 11
PHP Question

MySQL Group By or Order By

I have a mysql table

C1 - C2
A - 25.6
B - 22.5
C - 19.5
E - 15.6
A - 18.5
B - 20.5
F - 18.2
D - 19.6
F - 16.1
E - 20.6


And I need to order DESC c2, without repeating c1

A - 25.6
B - 22.5
E - 20.6
D - 19.6
F - 18.2


I have so far

for ($i=1; $i<=5; $i++) {
$query = mysql_query("SELECT * FROM myTable WHERE date='$SL_DATE' ORDER BY c2 DESC LIMIT $i",$db);
while ($row = mysql_fetch_array($query)) {
$c2_[$i] = $row[c2];
}
}


Any ideas?

Answer

I think this query will work:

SELECT C1, max(C2) as maxC2 FROM myTable WHERE date='$SL_DATE' group by C1 order by maxC2 DESC
Comments