douhuiyan2772 2017-02-14 10:38
浏览 68
已采纳

MySQL - 当有多个ID时,我需要选择列中的最大数字

Take the following dataset:

id | Number
 1 | 6534
 1 | 765
 1 | 1234
 2 | 744
 2 | 6109
 3 | 333
 3 | 9888
 3 | 3112
 3 | 98432

I want to show the highest Number for each id.

So Like this:

id | Number
 1 | 6534
 2 | 6109
 3 | 98432

How can I do this with a SELECT statement?

I've already tried the following:

SELECT * FROM mytable ORDER BY id, Number Desc

But this shows the entire dataset.

I'm not trying to get the number of occurences. I am trying to get the highest Number grouped by id but can't get it to work.

  • 写回答

3条回答 默认 最新

  • dongnue2071 2017-02-14 10:48
    关注

    You can try :

    Select id,max(Number) from Your_Table group by id
    

    Your Query that you have tried, it will only order your Data table by the given parameters.

    In the meantime, What i have proposed to you, will select the two columns you want to diplay (the id, and the maximum of the column "Number").

    And the group by will help to the maximum of each group. That's why a group by id is the right clause to have the maximum of each group of Ids.

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

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线