Mysql group by问题

喜欢ヅ旅行 2024-04-20 08:17 126阅读 0赞

看如下例子

  1. mysql> select count(1),task_type,status from action where biz_date='20141014' group by status;
  2. +----------+-----------+--------+
  3. | count(1) | task_type | status |
  4. +----------+-----------+--------+
  5. | 1 | shell | fail |
  6. | 16 | shell | succ |
  7. +----------+-----------+--------+
  8. 2 rows in set (0.00 sec)

mysql> select count(1),task_type,status from action where biz_date=’20141014’ group by task_type,status;

  1. +----------+-----------+--------+
  2. | count(1) | task_type | status |
  3. +----------+-----------+--------+
  4. | 2 | odps_sql | succ |
  5. | 1 | shell | fail |
  6. | 14 | shell | succ |
  7. +----------+-----------+--------+
  8. 3 rows in set (0.00 sec)

mysql> select count(1),task_type from action where biz_date=’20141014’ group by status;

  1. +----------+-----------+
  2. | count(1) | task_type |
  3. +----------+-----------+
  4. | 1 | shell |
  5. | 16 | shell |
  6. +----------+-----------+
  7. 2 rows in set (0.00 sec)

这在mysql 中是合法的。由于group by 和select 列不一致,在ORACLE中会报错。

得知这是mysql的设计,当group by 字段和select 多字段是唯一的一个分组时,这样能提高效率。

但是如果不能唯一确定的话,mysql会随机选一个值。这是要注意的。

个人觉的这更是个坑。。

这个功能是MySQL对标准sql的扩展,使用的时候要注意

分别为两个字段进行排序

20200108174949675.png

发表评论

表情:
评论列表 (有 0 条评论,126人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MySQL GROUP BY

    1. 只会返回分组后的一条结果,但是实际上里面含有的结果不仅有一条 \\![加粗样式][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow

    相关 MYSQLGroup By

    概述 `Group By`即根据其指定的规则对查询数据进行分组,这个分组的意义是对分组内数据进行数据处理,包括SUM、COUNT等等。 这里需要注意的是:`Selec

    相关 mysql group by order by

    这两天让一个数据查询难了。主要是对group by 理解的不够深入。才出现这样的情况 这种需求,我想很多人都遇到过。下面是我模拟我的内容表 复制代码 代码如下: