zoukankan      html  css  js  c++  java
  • MySQL 里面的Where 和Having和Count 和distinct和Group By对比

    mysql> select accid as uid,date(datetime) AS datetime from game.logLogin GROUP BY accid HAVING datetime='2013-8-20';
    +---------+------------+
    | uid     | datetime   |
    +---------+------------+
    | 1000010 | 2013-08-20 |
    | 1000012 | 2013-08-20 |
    +---------+------------+
    2 rows in set (0.00 sec)

    而实际的例子是

    mysql> select accid as uid,date(datetime) AS datetime from game.logLogin HAVING datetime='2013-8-20';
    +---------+------------+
    | uid     | datetime   |
    +---------+------------+
    | 1000004 | 2013-08-20 |
    | 1000004 | 2013-08-20 |
    | 1000001 | 2013-08-20 |
    | 1000000 | 2013-08-20 |
    | 1000004 | 2013-08-20 |
    | 1000004 | 2013-08-20 |
    | 1000012 | 2013-08-20 |
    | 1000010 | 2013-08-20 |
    | 1000000 | 2013-08-20 |
    | 1000002 | 2013-08-20 |
    | 1000006 | 2013-08-20 |
    | 1000003 | 2013-08-20 |
    | 1000003 | 2013-08-20 |
    | 1000012 | 2013-08-20 |
    | 1000003 | 2013-08-20 |
    |       0 | 2013-08-20 |
    | 1000012 | 2013-08-20 |
    +---------+------------+
    17 rows in set (0.00 sec)

    用大腿想都会不对

    mysql> select distinct accid as uid from (select accid,date(datetime) AS datetime from game.logLogin HAVING datetime='2013-8-20') as t;
    +---------+
    | uid     |
    +---------+
    | 1000004 |
    | 1000001 |
    | 1000000 |
    | 1000012 |
    | 1000010 |
    | 1000002 |
    | 1000006 |
    | 1000003 |
    |       0 |
    +---------+
    9 rows in set (0.00 sec)

    当然如何不用 HAVING 和 DISTINCT 和 COUNT 还有GROUP By 的话是可以找出记录的

    mysql> select accountID as uid,date(signTime) AS signTime from platform.account HAVING signTime='2013-8-20';
    +---------+------------+
    | uid     | signTime   |
    +---------+------------+
    | 1000013 | 2013-08-20 |
    | 1000014 | 2013-08-20 |
    +---------+------------+
    2 rows in set (0.00 sec)
    
    mysql> select accountID as uid,date(signTime) AS signTime from platform.account HAVING signTime='2013-8-19';
    +---------+------------+
    | uid     | signTime   |
    +---------+------------+
    | 1000000 | 2013-08-19 |
    | 1000001 | 2013-08-19 |
    | 1000002 | 2013-08-19 |
    | 1000003 | 2013-08-19 |
    | 1000004 | 2013-08-19 |
    | 1000005 | 2013-08-19 |
    | 1000006 | 2013-08-19 |
    | 1000007 | 2013-08-19 |
    | 1000008 | 2013-08-19 |
    | 1000009 | 2013-08-19 |
    | 1000010 | 2013-08-19 |
    | 1000011 | 2013-08-19 |
    | 1000012 | 2013-08-19 |
    +---------+------------+
    13 rows in set (0.00 sec)
  • 相关阅读:
    Servlet 易错点和注意点
    Spring 完成自动注入(autowire)
    Java 定时调度Timer&Quartz
    常用Linux命令杂记
    Spring 使用AOP——基于注解配置
    Spring 使用AOP——xml配置
    Spring 使用纯注解方式完成IoC
    Spring 简单使用IoC与DI——XML配置
    让多个HTML页面 使用 同一段HTML代码
    Feture、ListenableFuture、ComplatableFuture
  • 原文地址:https://www.cnblogs.com/jackluo/p/3269978.html
Copyright © 2011-2022 走看看