MySQL return first row of a joined table

前端 未结 5 629
庸人自扰
庸人自扰 2021-01-04 13:19

I have two tables (country & ducks) where the country table has every country in the world and the ducks table has a list of ducks with a country_id field to link to the

5条回答
  •  时光取名叫无心
    2021-01-04 13:58

    Try this:

    SELECT c.country, MAX(d.rating) AS max_rating
    FROM country c
    JOIN ducks d ON c.id = d.country_id
    GROUP BY c.id
    ORDER BY c.country ASC
    

    If the "highest rating" is 1, then change MAX(d.rating) to MIN(d.rating)

提交回复
热议问题