t2是用下面的表格创立的。
create table t2 (
  id int primary key,
  gid char,
  col1 int,
  col2 int
) engine=myisam;insert into t2 values  
(1,'A',31,6),
(2,'B',25,83),
(3,'C',76,21),
(4,'D',63,56),
(5,'E',3,17),
(6,'A',29,97),
(7,'B',88,63),
(8,'C',16,22),
(9,'D',25,43),
(10,'E',45,28),
(11,'A',2,78),
(12,'B',30,79),
(13,'C',96,73),
(14,'D',37,40),
(15,'E',14,86),
(16,'A',32,67),
(17,'B',84,38),
(18,'C',27,9),
(19,'D',31,21),
(20,'E',80,63),
(21,'A',89,9),
(22,'B',15,22),
(23,'C',46,84),
(24,'D',54,79),
(25,'E',85,64),
(26,'A',87,13),
(27,'B',40,45),
(28,'C',34,90),
(29,'D',63,8),
(30,'E',66,40),
(31,'A',83,49),
(32,'B',4,90),
(33,'C',81,7),
(34,'D',11,12),
(35,'E',85,10),
(36,'A',39,75),
(37,'B',22,39),
(38,'C',76,67),
(39,'D',20,11),
(40,'E',81,36);
t2 是这样建立的,请看看,select * from t2 group by gid ;它的输出结果我想不通,请帮助我一下
输出的是:
select * from t2 group by gid;
+----+------+------+------+
| id | gid | col1 | col2 |
+----+------+------+------+
| 1 | A | 31 | 6 |
| 2 | B | 25 | 83 |
| 3 | C | 76 | 21 |
| 4 | D | 63 | 56 |
| 5 | E | 3 | 17 |
+----+------+------+------+
5 rows in set (0.00 sec)
为何不输出下面:
| id | gid | col1 | col2 |
+----+------+------+------+
| 6 | A | 29 | 97 |
| 7 | B | 88 | 63 |
| 8 | C | 16 | 22 |
| 9 | D | 25 | 43 |
| 10 | E | 45 | 28 |
+----+------+------+------+

解决方案 »

  1.   

    你的SQL语句不是标准的SQL语句
    MySQL 扩展了 GROUP BY的用途,因此你可以使用SELECT 列表中不出现在GROUP BY语句中的列或运算。这代表 “对该组的任何可能值 ”。你可以通过避免排序和对不必要项分组的办法得到它更好的性能。例如,在下列问询中,你无须对customer.name 进行分组: mysql> SELECT order.custid, customer.name, MAX(payments)    ->        FROM order,customer    ->        WHERE order.custid = customer.custid    ->        GROUP BY order.custid;在标准SQL中, 你必须将 customer.name添加到 GROUP BY子句中。在MySQL中, 假如你不在ANSI模式中运行,则这个名字就是多余的。
      

  2.   

    对gid分组后,对每一组的其他数据进行处理
      

  3.   

    输出中不在GROUP BY中的非聚合字段,则MYSQL会自由的选择一个该列中值输出。select * from t2 group by gid id | gid | col1 | col2 | 中除了gid 值外,其它值由MYSQL自行决定找一个值输出。一般情况下,MYSQL会取这一列的第一个值。