› 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 · Sep 20, 2015 · 4425 views
    This topic created in 4026 days ago, the information mentioned may be changed or developed.
    现有一个表 用来记录某软件的客户端登录日志
    字段有
    time,username,ip,hwid

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

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

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

    请问这样的 sql 应该怎么写呢?
    Supplement 1  ·  Sep 21, 2015
    谢谢各位
    集合各楼的代码以及提供的信息
    现使用这个 sql 完美实现
    $sql = "select count (distinct hwid ) as times,user from $db_table group by user order by times desc";
    13 replies  •  2015-09-20 21:42:37 +08:00
    asxalex
        1
    asxalex  
       Sep 20, 2015   ❤️ 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  
       Sep 20, 2015   ❤️ 1
    select tab.username,count (tab.hwid ) from (select username,hwid from test group by hwid ) tab group by tab.username;
    cxbig
        3
    cxbig  
       Sep 20, 2015   ❤️ 1
    没有楼上没那么复杂吧
    SELECT username, count (hwid )
    FROM table
    GROUP BY username
    SilentDepth
        4
    SilentDepth  
       Sep 20, 2015   ❤️ 1
    @cxbig 同感。再加个 ORDER BY 就更好了
    coosir
        5
    coosir  
       Sep 20, 2015   ❤️ 1
    @cxbig @SilentDepth 额,这样子同终端的多次登录都会 count 进去呀
    F281M6Dh8DXpD1g2
        6
    F281M6Dh8DXpD1g2  
       Sep 20, 2015
    伸手党可耻
    echo1937
        7
    echo1937  
       Sep 20, 2015   ❤️ 1


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

    这样的话如 8 楼 @soulgeek 所说, COUNT (DISTINCT hwid )就可以了
    cxbig
        11
    cxbig  
       Sep 20, 2015   ❤️ 1
    @SilentDepth 楼上正解,我记错了
    SilentDepth
        12
    SilentDepth  
       Sep 20, 2015   ❤️ 1
    @cxbig 左括号前加空格可以理解,为什么右括号前也要加……什么时候回复可以支持代码展示啊 :sigh:
    cxbig
        13
    cxbig  
       Sep 20, 2015
    @SilentDepth 自动给的,我没有办法。 T-T
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3274 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 00:37 · PVG 08:37 · LAX 17:37 · JFK 20:37
    ♥ Do have faith in what you're doing.