当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL高级查询之与Group By集合使用介绍

MySQL高级查询之与Group By集合使用介绍

2017年12月12日  | 移动技术网IT编程  | 我要评论

group_concat

mysql> select student_name,
    ->     group_concat(test_score)
    ->     from student
    ->     group by student_name;

or:

mysql> select student_name,
    ->     group_concat(distinct test_score
    ->               order by test_score desc separator ' ')
    ->     from student
    ->     group by student_name;

在mysql中,你可以获取表达式组合的连接值。你可以使用distinct删去重复值。假若你希望多结果值进行排序,则应该使用  order by子句。若要按相反顺序排列,将 desc (递减) 关键词添加到你要用order by 子句进行排序的列名称中。默认顺序为升序;可使用asc将其明确指定。   separator 后面跟随应该被插入结果的值中间的字符串值。默认为逗号 (‘,')。通过指定separator '' ,你可以删除所有分隔符。

ps:就是可以在一个语句中得到 group by 被 聚合的项的每个子值的一个组合的字符串

 2 with rollup

group by子句允许一个将额外行添加到简略输出端 with rollup 修饰符。这些行代表高层(或高聚集)简略操作。rollup 因而允许你在多层分析的角度回答有关问询的问题

或者你可以使用 rollup, 它能用一个问询提供双层分析。将一个 with rollup修饰符添加到group by 语句,使询问产生另一行结果,该行显示了所有年份的总价值:

mysql> select year, sum(profit) from sales group by year with rollup;

+------+-------------+

| year | sum(profit) |

+------+-------------+

| 2000 |        4525 |

| 2001 |        3010 |

| null |        7535 |

+------+-------------+

总计高聚集行被年份列中的null值标出。

当有多重 group by 列时,rollup产生的效果更加复杂。这时,每次在除了最后一个分类列之外的任何列出现一个 “break” (值的改变) ,则问讯会产生一个高聚集累计行。

例如,在没有 rollup的情况下,一个以年、国家和产品为基础的关于 sales 表的一览表可能如下所示:

mysql> select year, country, product, sum(profit)

    -> from sales

    -> group by year, country, product;

+------+---------+------------+-------------+

| year | country | product    | sum(profit) |

+------+---------+------------+-------------+

| 2000 | finland | computer   |        1500 |

| 2000 | finland | phone      |         100 |

| 2000 | india   | calculator |         150 |

| 2000 | india   | computer   |        1200 |

| 2000 | usa     | calculator |          75 |

| 2000 | usa     | computer   |        1500 |

| 2001 | finland | phone      |          10 |

| 2001 | usa     | calculator |          50 |

| 2001 | usa     | computer   |        2700 |

| 2001 | usa     | tv         |         250 |

+------+---------+------------+-------------+

表示总值的输出结果仅位于年/国家/产品的分析级别。当添加了 rollup后, 问询会产生一些额外的行:

mysql> select year, country, product, sum(profit)

    -> from sales

    -> group by year, country, product with rollup;

+------+---------+------------+-------------+

| year | country | product    | sum(profit) |

+------+---------+------------+-------------+

| 2000 | finland | computer   |        1500 |

| 2000 | finland | phone      |         100 |

| 2000 | finland | null       |        1600 |

| 2000 | india   | calculator |         150 |

| 2000 | india   | computer   |        1200 |

| 2000 | india   | null       |        1350 |

| 2000 | usa     | calculator |          75 |

| 2000 | usa     | computer   |        1500 |

| 2000 | usa     | null       |        1575 |

| 2000 | null    | null       |        4525 |

| 2001 | finland | phone      |          10 |

| 2001 | finland | null       |          10 |

| 2001 | usa     | calculator |          50 |

| 2001 | usa     | computer   |        2700 |

| 2001 | usa     | tv         |         250 |

| 2001 | usa     | null       |        3000 |

| 2001 | null    | null       |        3010 |

| null | null    | null       |        7535 |

+------+---------+------------+-------------+

当你使用 rollup时, 你不能同时使用 order by子句进行结果排序。换言之, rollup 和order by 是互相排斥的。然而,你仍可以对排序进行一些控制。在 mysql中, group by 可以对结果进行排序,而且你可以在group by列表指定的列中使用明确的 asc和desc关键词,从而对个别列进行排序。 (不论如何排序被rollup添加的较高级别的总计行仍出现在它们被计算出的行后面)。

limit可用来限制返回客户端的行数。limit 用在 rollup后面, 因此这个限制 会取消被rollup添加的行

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网