dqk94069 2011-08-31 10:34
浏览 37
已采纳

MySQL选择分组依据和排序依据

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 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.

  • 写回答

2条回答 默认 最新

  • douling8087 2011-08-31 10:36
    关注

    Change your GROUP BY to:

    GROUP BY surname, forename
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见