【问题标题】:mySQl - How to use MAX() function with other query attributesmySQl - 如何将 MAX() 函数与其他查询属性一起使用
【发布时间】:2021-07-29 05:42:55
【问题描述】:

下午好: 给定某个数据库,我正在尝试以下 SQL 查询:

select id, date, shop, max(sold_units) from storage where id='9911110810110319890031009710810111097';

上面的 SQL 编译得很好,但我遇到了以下情况:列 max(sold_units) 显示最大值(正确),但是,其他属性(id、date、shop)正在引用另一行,即不正确(因为其他箭头没有 sold_units 的最大值)。如何修改上面的查询,使属性“id”、“date”、“shop”与 max(sold_units) 位于同一行。

换句话说,它应该显示 id、date、shop 和 max(sold_units) 从具有最大销售单位数量的行中。

感谢您的关注

【问题讨论】:

    标签: mysql sql select max


    【解决方案1】:

    您的查询格式不正确。它应该会产生语法错误,因为 select 有 max() 所以这是一个聚合查询。但是没有group by。 select 也有未聚合的列。

    我怀疑你打算:

    select id, date, shop, sold_units
    from storage s
    where id = '9911110810110310511110897109971031009710810111097'
    order by sold_units desc
    limit 1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-20
      • 2021-10-03
      • 2019-01-25
      • 2018-04-03
      • 2014-08-18
      • 2013-01-03
      • 2013-04-24
      • 1970-01-01
      相关资源
      最近更新 更多