SQL - SELECT MAX() and accompanying field

后端 未结 4 648
终归单人心
终归单人心 2020-12-31 23:41

What I have is basically a problem which is easily solved with multiple tables, but I have only a single table to do it.

Consider the following database table

<
4条回答
  •  天命终不由人
    2021-01-01 00:03

    I think I have a solution that's different from the ones already proposed:

    select *
    from foo
    where id = (
      select id
      from foo F
      where F.bar = foo.bar
      order by F.baz
      limit 1
    )
    

    This gives you all the foo records that have the greatest baz compared to other foo records with the same bar.

提交回复
热议问题