mysql查询一组中某列最大值对应行的记录

需求:取cover_percent 按从大到小取值,且每组category_id 只取一行记录
如下sql 含义:cover_percent 取最大的,且category_id 是去重的,每组category_id 只取一行记录

SELECT
        id,
        tag_name,
        fact_column_show,
        fact_column_type,
category_id,cover_percent
        FROM tag_info a
        where
        bisType = 0
        AND tag_type IN (1, 2, 7)
        and fact_column_show is not null
        and fact_column_type is not null
     
        AND NOT EXISTS (
            SELECT
            1
            FROM
            tag_info b
            WHERE
            a.category_id = b.category_id
            AND b.cover_percent > a.cover_percent
            )
        ORDER BY cover_percent DESC
        LIMIT 0, 8


    原文作者:tszxlzc
    原文地址: https://blog.csdn.net/tszxlzc/article/details/88647887
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞