MySQL Select Group By and Order By
Here is the SQL Statement that I am using..
$sql = "SELECT surname, forename, count(*) FROM people WHERE user_id='$user_id'
GROUP BY surname ORDER BY time";
$result = mysql_query($sql);
$count=mysql_num_rows($result);
if($count > 0){
while($row = mysql_fetch_array($result)) {
$i = $i +1;
$person[$i] = $row['surname'].'&&'.$row['forename'];
}
What 开发者_JS百科I would like to get is all the entries from the people database relative to user_id grouped by surname and ordered by the time that they were entered in to the database. e.g.
12:00 Smith James
13:00 Smith Linda
14:00 Smith Sam
12:30 OtherSurname Lydia
13:30 OtherSurname Harry
14:30 OtherSurname Phil
The problem at the moment is that I only get one result for each surname. How can I use the SELECT statement with GROUP BY and ORDER BY to get all of the entries? thank you.
Change your GROUP BY to:
GROUP BY surname, forename
You don't need to group by surname, you need to order by surname
ORDER BY surname,time
精彩评论