示例表(scores)如下:
cName      score
AAA         50
BBB         100
查询语句:mysql> select cname,sum(score) from scores;
+-------+------------+
| cname | sum(score) |
+-------+------------+
| AAA   |        150 |
+-------+------------+
1 row in set (0.00 sec)在sql server中这句应该是会报错的。
是mysql的设置问题吗?

解决方案 »

  1.   

    有这回事,我的mysql就不可以这样查询。
      

  2.   

    select cname,sum(score) from scores group by cname;
      

  3.   

    回2、3楼:
    我知道加上group by 是正确的。但是mysql可以设置成不能这样查询吗?mysql要怎样设置?
      

  4.   

    sql mode 设置为 ansi5.1.7. Server SQL ModesANSI 
    This mode changes syntax and behavior to conform more closely to standard SQL. 
      

  5.   

    12.10.3. 具有隐含字段的GROUP BY
    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模式中运行,则这个名字就是多余的。 假如你从 GROUP BY 部分省略的列在该组中不是唯一的,那么不要使用这个功能! 你会得到非预测性结果。在有些情况下,你可以使用MIN()和MAX() 获取一个特殊的列值,即使他不是唯一的。下面给出了来自包含排序列中最小值的列中的值: SUBSTR(MIN(CONCAT(RPAD(sort,6,' '),column)),7)See 3.6.4节,“拥有某个字段的组间最大值的行”. 注意,假如你正在尝试遵循标准 SQL, 你不能使用GROUP BY或 ORDER BY子句中的表达式。你可以通过使用表达式的别名绕过这一限制:
      

  6.   

    为什么我将sql_mode变量设置为ANSI还是可以查询,结果还是如我在一楼上贴出来的那样。
      

  7.   

    mysql> set @@sql_mode='ONLY_FULL_GROUP_BY';
    Query OK, 0 rows affected (0.00 sec)mysql> select name,sum(num) from a;
    ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GR
    OUP columns is illegal if there is no GROUP BY clause
    mysql>
      

  8.   

    mysql> select @@sql_mode;
    +----------------------------------------------------------------+
    | @@sql_mode                                                     |
    +----------------------------------------------------------------+
    | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
    +----------------------------------------------------------------+
    1 row in set (0.00 sec)mysql> select name,sum(num) from a;
    +------+----------+
    | name | sum(num) |
    +------+----------+
    | NULL |     NULL |
    +------+----------+
    1 row in set (0.00 sec)mysql> set @@sql_mode=concat(@@sql_mode,',ONLY_FULL_GROUP_BY');
    Query OK, 0 rows affected (0.00 sec)mysql> select @@sql_mode;
    +-----------------------------------------------------------------------------------+
    | @@sql_mode                                                                        |
    +-----------------------------------------------------------------------------------+
    | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
    +-----------------------------------------------------------------------------------+
    1 row in set (0.00 sec)mysql> select name,sum(num) from a;
    ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GR
    OUP columns is illegal if there is no GROUP BY clause
    mysql>
      

  9.   

    mysql> select SUBSTR(MIN(CONCAT(RPAD(userid,6,' '),username)),7) from user;
    +----------------------------------------------------+
    | SUBSTR(MIN(CONCAT(RPAD(userid,6,' '),username)),7) |
    +----------------------------------------------------+
    | 螟ァ譏ッ荳ェ                                             |
    +----------------------------------------------------+
    1 row in set (0.01 sec)mysql> select username from user where userid=(select min(userid) from user);
    +-----------+
    | username  |
    +-----------+
    | 螟ァ譏ッ荳ェ    |
    +-----------+
    1 row in set (0.02 sec)
    好强悍的想法
      

  10.   

    mysql> select @@sql_mode;
    +----------------------------------------------------------------+
    | @@sql_mode                                                     |
    +----------------------------------------------------------------+
    | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
    +----------------------------------------------------------------+
    1 row in set (0.03 sec)
    我的sqlmode