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
bestsanmao
V2EX  ›  MySQL

构建 SQL 语句求助

  •  
  •   bestsanmao · 2015-09-20 17:03:57 +08:00 · 3071 次点击
    这是一个创建于 3112 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现有一个表 用来记录某软件的客户端登录日志
    字段有
    time,username,ip,hwid

    hwid 意思是 hardware id ,也就是软件登录时所在的终端的机器码
    现在我想做这么个查询:

    查询出来的结果类似于这样
    张三,10
    李四,8
    王五,5

    也就是说,查询各个用户名在多少台终端上登录过(不同的 hwid 值代表不同的终端),并以台数进行排序
    上面就表示张三在 10 台终端上登录过,李四在 8 台终端上登录过

    请问这样的 sql 应该怎么写呢?
    第 1 条附言  ·  2015-09-21 10:27:43 +08:00
    谢谢各位
    集合各楼的代码以及提供的信息
    现使用这个 sql 完美实现
    $sql = "select count (distinct hwid ) as times,user from $db_table group by user order by times desc";
    13 条回复    2015-09-20 21:42:37 +08:00
    asxalex
        1
    asxalex  
       2015-09-20 17:27:27 +08:00   ❤️ 1
    select tmp.username, count (1 ) cnt from (select username, count (1 ) from table_name group by username, hwid ) tmp group by tmp.username order by cnt desc;
    ttcool
        2
    ttcool  
       2015-09-20 17:33:38 +08:00   ❤️ 1
    select tab.username,count (tab.hwid ) from (select username,hwid from test group by hwid ) tab group by tab.username;
    cxbig
        3
    cxbig  
       2015-09-20 17:40:33 +08:00   ❤️ 1
    没有楼上没那么复杂吧
    SELECT username, count (hwid )
    FROM table
    GROUP BY username
    SilentDepth
        4
    SilentDepth  
       2015-09-20 18:11:37 +08:00   ❤️ 1
    @cxbig 同感。再加个 ORDER BY 就更好了
    coosir
        5
    coosir  
       2015-09-20 18:17:31 +08:00   ❤️ 1
    @cxbig @SilentDepth 额,这样子同终端的多次登录都会 count 进去呀
    liprais
        6
    liprais  
       2015-09-20 18:56:41 +08:00
    伸手党可耻
    echo1937
        7
    echo1937  
       2015-09-20 19:16:35 +08:00   ❤️ 1


    MySQL 的写法.
    soulgeek
        8
    soulgeek  
       2015-09-20 19:23:21 +08:00   ❤️ 1
    3 楼的 count 加个 distinct 就好了
    cxbig
        9
    cxbig  
       2015-09-20 19:55:20 +08:00   ❤️ 1
    了解了,用 time 来做 count
    SELECT DISTINCT username, hwid, count ($time )
    FROM table
    GROUP BY username, hwid
    SilentDepth
        10
    SilentDepth  
       2015-09-20 20:31:58 +08:00   ❤️ 2
    @cxbig 楼主的意思是要统计各 username 对应了多少种 hwid

    这样的话如 8 楼 @soulgeek 所说, COUNT (DISTINCT hwid )就可以了
    cxbig
        11
    cxbig  
       2015-09-20 20:34:40 +08:00   ❤️ 1
    @SilentDepth 楼上正解,我记错了
    SilentDepth
        12
    SilentDepth  
       2015-09-20 20:39:54 +08:00   ❤️ 1
    @cxbig 左括号前加空格可以理解,为什么右括号前也要加……什么时候回复可以支持代码展示啊 :sigh:
    cxbig
        13
    cxbig  
       2015-09-20 21:42:37 +08:00
    @SilentDepth 自动给的,我没有办法。 T-T
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1646 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 16:48 · PVG 00:48 · LAX 09:48 · JFK 12:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.