select a.a1, a.a2 from A a group by a.a1, a.a2; a.a1    a.a2 
---------------- 
 1        1 
 1        2 
 1        3 
 2        1 
 2        2计算记录总个数(如上个数为5), 怎么解决?说明下,不要使用嵌套查询 如:
select count(1) from (select 1 from A a group by a.a1, a.a2) tmp;
求一条最高效的SQL语句。问题解决,马上结贴。谢谢!在线等了

解决方案 »

  1.   

    select count(distinct a1,a2) from A
      

  2.   

    加入自增字段ID
    SELECT COUNT(*) from tdbf a 
    where not exists(select 1 from tdbf b where a.aa1&a.aa2=b.aa1&b.aa2 and a.id>b.id)
      

  3.   

    OR
    SELECT COUNT(*) FROM (
    SELECT AA1,AA2 FROM TDBF
    UNION
    SELECT AA1,AA2 FROM TDBF)
    通过UNION去掉重复记录
      

  4.   


    自增字段ID是有,当数据量很大的时候,还是要等很长时间,最后查出来的结果为1,还有a1 a2里面存储的是字符串, 不知道a.aa1&a.aa2=b.aa1&b.aa2 是怎么执行的。
      

  5.   


    Mysql 提示 Every derived table must have its own alias
      

  6.   

    SELECT COUNT(*) FROM (
    SELECT AA1,AA2 FROM TDBF
    UNION
    SELECT AA1,AA2 FROM TDBF) a
      

  7.   


    还是太慢了, 感觉都复杂化了, 还没下面这样快
    select count(1) from (select 1 from A a group by a.a1, a.a2) tmp;
    我表中数据有上千万,而且a1,a2均为字符串类型,长度在10左右,而且查询时都用到了分区和索引
      

  8.   


    还要select count(1) from (你的SQL语句) tmp; 这样才行
      

  9.   


    就是感觉 select count(*) from (select 1 ....) 不太好。当个数非常多时, 后面的 select 1 ... 太耗时了。 就是想在分组之后就直接统计个数,而不是用1来代替然后统计1的个数
      

  10.   

    用SELECT 主键代替SELECT 1试试
      

  11.   

    mysql> select * from a;
    +------+------+
    | a1   | a2   |
    +------+------+
    |    1 |    1 |
    |    1 |    2 |
    |    1 |    3 |
    |    2 |    1 |
    |    2 |    2 |
    +------+------+
    5 rows in set (0.00 sec)mysql> select count(*) from a;
    +----------+
    | count(*) |
    +----------+
    |        5 |
    +----------+
    1 row in set (0.00 sec)mysql> select count(distinct a1,a2) from a;
    +-----------------------+
    | count(distinct a1,a2) |
    +-----------------------+
    |                     5 |
    +-----------------------+
    1 row in set (0.00 sec)mysql>为什么 "还要select count(1) from (你的SQL语句) tmp; 这样才行"
     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  12.   

    正解。测试如下:mysql> select * from test1;
    +------+------+
    | a1   | a2   |
    +------+------+
    | 1    | 1    |
    | 1    | 2    |
    | 1    | 3    |
    | 2    | 1    |
    | 2    | 2    |
    | 1    | 1    |
    | 1    | 2    |
    | 1    | 3    |
    | 2    | 1    |
    | 2    | 2    |
    +------+------+
    10 rows in set (0.00 sec)mysql> select a1,a2 from test1 group by a1,a2;
    +------+------+
    | a1   | a2   |
    +------+------+
    | 1    | 1    |
    | 1    | 2    |
    | 1    | 3    |
    | 2    | 1    |
    | 2    | 2    |
    +------+------+
    5 rows in set (0.00 sec)mysql> select count(distinct a1,a2) from test1;
    +-----------------------+
    | count(distinct a1,a2) |
    +-----------------------+
    |                     5 |
    +-----------------------+
    1 row in set (0.00 sec)
      

  13.   

    CREATE TABLE `a` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `a1` varchar(10) DEFAULT NULL,
      `a2` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;insert into a values (1,'1','1'), (2,'1','2'),(3,'1','3'),(4,'2','1'),(5,'2','2');
    select * from a;
    select count(distinct a1,a2) from a group by a1,a2;运行结果:
    count(distinct a1,a2)
    ------------------------
    1
    1
    1
    1
    1你的SQL掉了 group by a1,a2 这部分
      

  14.   

    既然一定要 group by 就随便加个变量 @x 
    mysql> select count(distinct a1,a2) from a group by @x;
    +-----------------------+
    | count(distinct a1,a2) |
    +-----------------------+
    |                     5 |
    +-----------------------+
    1 row in set (0.00 sec)mysql>
      

  15.   

    还是有问题,帖子就到这里结了。重新开了个贴:
    http://topic.csdn.net/u/20100312/16/1e4578fa-7d86-4693-8b9f-8b9079dd3718.html