这个mysql查询从评论中选择最大值,但是insId是重复的,但是我想删除重复的insId。

SELECT reviews.id, reviews_max.insId, reviews_max.maxAvgRating
FROM
(
    SELECT insId, MAX(avgRating) AS maxAvgRating
    FROM reviews
    WHERE status = '1'
    GROUP BY insId
) AS reviews_max
INNER JOIN reviews
    ON reviews.insId = reviews_max.insId AND
       reviews.avgRating = reviews_max.maxAvgRating

最佳答案

没有这样的子选择,您能否做到这一点?

SELECT MAX(reviews.id) as id, MAX(reviews.insId), MAX(reviews.avgRating) AS maxAvgRating
FROM reviews
WHERE status = 1
GROUP BY reviews.id, reviews.insId


如果愿意,您仍然可以将id复制为insId,但是该值应该已经在id字段中。

我使用SQLFiddle创建了一个简单的示例:http://sqlfiddle.com/#!9/5b6421/2/0

关于mysql - 如何在自我内部联接mysql查询中选择不同的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50810066/

10-10 03:32