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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tszxlzc/article/details/88647887

需求:取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


展开阅读全文

没有更多推荐了,返回首页