CREATE TABLE IF NOT EXISTS `custorders` (
  `idcustorder` int(11) NOT NULL auto_increment,
  `idcustomer` int(11) NOT NULL,
  PRIMARY KEY  (`idcustorder`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ;--
-- 导出表中的数据 `custorders`
--INSERT INTO `custorders` (`idcustorder`, `idcustomer`) VALUES
(1, 10),
(2, 10),
(3, 11),
(4, 11),
(5, 11),
(6, 12),
(7, 13),
(8, 14),
(9, 15),
(10, 16);
请问如何根据idcustomer汇总查询出记录有多少条。我想查出的结果汇总记录是 7 条,因为10 、 11都有重复的记录。我想表述的情况如下(但下面的SQL语句错误):
select sum(count(*)) as 数量 from custorders group by idcustomer

解决方案 »

  1.   

    mysql> select * from custorders;
    +-------------+------------+
    | idcustorder | idcustomer |
    +-------------+------------+
    |           1 |         10 |
    |           2 |         10 |
    |           3 |         11 |
    |           4 |         11 |
    |           5 |         11 |
    |           6 |         12 |
    |           7 |         13 |
    |           8 |         14 |
    |           9 |         15 |
    |          10 |         16 |
    +-------------+------------+
    10 rows in set (0.05 sec)mysql> select count(distinct idcustomer) from custorders;
    +----------------------------+
    | count(distinct idcustomer) |
    +----------------------------+
    |                          7 |
    +----------------------------+
    1 row in set (0.08 sec)mysql>
      

  2.   

    select count(distinct idcustomer) from custorders