V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
anai1943
V2EX  ›  MySQL

mysql 排序去重 sql 写法

  •  
  •   anai1943 · 2016-10-21 18:45:37 +08:00 · 5208 次点击
    这是一个创建于 2954 天前的主题,其中的信息可能已经有所发展或是发生改变。

    表结构如下

    data_id 主键

    user_id

    data_name

    hits 点击量

    每个用户有多条数据,查询前 8 个用户点击量最多的一条数据,再按照点击量倒序,这个 sql 怎么写,谢谢!

    第 1 条附言  ·  2016-10-21 19:20:07 +08:00
    | data_id | user_id | data_name |hits
    | 1 | 2 | test1 | 140
    | 2 | 2 | test2 | 200
    | 3 | 3 | test2 | 110
    | 4 | 3 | test2 | 10
    | 5 | 1 | test2 | 130
    | 6 | 4 | test2 | 10
    | 7 | 4 | test2 | 100

    假如要查询出来下面的 3 条结果,也就是前 3 条点击量最多的数据, user_id 不能重复

    | data_id | user_id | data_name |hits
    | 2 | 2 | test2 | 200
    | 5 | 1 | test2 | 130
    | 3 | 3 | test2 | 110

    sql 怎么写,再次谢谢!
    第 2 条附言  ·  2016-10-21 23:52:01 +08:00

    感谢 @kfll @akira @Powered

    最终用 @Powered 的写法查询成功,谢谢!

    SELECT
    	t.*
    FROM
    	(
    		SELECT
    			user_id,
    			max(hits) AS max_hits
    		FROM
    			t
    		GROUP BY
    			user_id
    	) t2
    LEFT JOIN t ON t.user_id = t2.user_id
    AND t.hits = t2.max_hits
    ORDER BY
    	t2.max_hits DESC
    LIMIT 3
    
    8 条回复    2016-10-21 20:08:26 +08:00
    cxbig
        1
    cxbig  
       2016-10-21 18:56:17 +08:00 via iPhone
    SELECT data_name, count(hits)
    FROM table
    GROUP BY data_name
    ORDER BY count(hits) DESC
    anai1943
        2
    anai1943  
    OP
       2016-10-21 19:21:05 +08:00
    @cxbig 您看下附言,我想查询的是这样的结果,谢谢!
    lishunan246
        3
    lishunan246  
       2016-10-21 19:30:52 +08:00
    select distinct
    kfll
        4
    kfll  
       2016-10-21 19:37:03 +08:00 via iPhone   ❤️ 1
    select * from (select user_id, max(hits) as max_hits from t group by user_id) t2 left join t on t.user_id = t2.user_id and t.hits = t2.max_hits

    这样?
    akira
        5
    akira  
       2016-10-21 19:52:30 +08:00   ❤️ 1
    @kfll
    select t.* from (select user_id, max(hits) as max_hits from t group by user_id) t2 left join t on t.user_id = t2.user_id and t.hits = t2.max_hits

    order by t.max_hits desc
    limit 3
    neoblackcap
        6
    neoblackcap  
       2016-10-21 19:59:38 +08:00
    MySQL 没有 windows 函数,无法实现你想要的效果,若是仅仅选取 user_id 跟 hits 两个 field 倒是可以做到
    Powered
        7
    Powered  
       2016-10-21 20:00:09 +08:00   ❤️ 1
    @akira

    select t.* from (select user_id, max(hits) as max_hits from t group by user_id) t2 left join t on t.user_id = t2.user_id and t.hits = t2.max_hits

    order by t2.max_hits desc
    limit 3
    reus
        8
    reus  
       2016-10-21 20:08:26 +08:00   ❤️ 1
    如果支持 window functions ,就很简单,好像 MariaDB 支持? postgres 支持,所以很方便

    select * from (

    select
    *, row_number() over (patition by user_id order by hits desc) as r
    from table

    )
    where r = 1
    order by hits desc
    limit 8
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1639 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 78ms · UTC 16:56 · PVG 00:56 · LAX 08:56 · JFK 11:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.