我有以下SQL:

SELECT
    Artist.name
    Product.id
FROM
    Artist
INNER JOIN
    Product
ON
    Product.artist_id = Artist.id
GROUP BY
    Artist.artist_id
ORDER BY
    Artist.last_name, Artist.first_name;


我需要返回的Product.id为最新记录,即每个艺术家的最高id。我该怎么做呢?

最佳答案

怎么样

SELECT
    Artist.name,
    max(Product.id)
FROM
    Artist
INNER JOIN
    Product
ON
    Product.artist_id = Artist.id
GROUP BY
    Artist.artist_id, Artist.name
ORDER BY
    Artist.last_name, Artist.first_name;


还是我误会了你的问题?

关于mysql - MySQL-如何订购联接表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8270763/

10-12 12:42