`
DavyJones2010
  • 浏览: 147992 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

MySQL: Group By

 
阅读更多

1. Group By

    1) GroupBy Is usually used with aggragation function(statistic function). If not, group by is pointless.

    2) Five aggragation function:

        1) max

        2) min

        3) sum

        4) avg

        5) count

    Eg.

# fetch the most expensive goods price
# knowing the process of fetching max
select max(shop_price) from goods;
select max(market_price - shop_price) from goods;

# select goods_id, goods_name, max(shop_price) from goods;
# the above will not work correctly
# this is a syntax error in Oracle/SQLServer

# fetch the most expensive goods price in each category
select cat_id, max(shop_price) from goods group by cat_id;
# cat_id makes sense as we are grouping by cat_id;

# fetch the max goods_id
select max(goods_id) from goods;

# fetch the min goods_id
select min(goods_id) from goods;

# fetch the cheapest shop_price
select min(shop_price) from goods;

# count the sum of goods_number
select sum(goods_number) from goods;

# count the sum of goods_number in each category
select cat_id, sum(goods_number) from goods group by cat_id;

# calculate the average shop_price
select avg(shop_price) from goods;

# count the variety of goods (different line of goods)
# count the registered user today
# is also applicable in paging: calculate the number of different lines in order to paging
select count(goods_id) from goods;
select count(*) from goods;

# count the variety of goods in each cat
select cat_id, count(*) from goods group by cat_id;

     Comment:

        1) Regard column name as variable:

                 select cat_id, count(*) from goods group by cat_id;

                 cat_id can be seen as variable.

                 variable can use operator to calculating.

    Eg

# Regard column name as variable

# Fetch the difference between shop_price and market_price
select (market_price - shop_price) from goods;

# Fetch the total money in each category
select cat_id, sum(goods_num * shop_price) from goods group by cat_id;

# Alias for a certain column
select cat_id, sum(goods_num * shop_price) as cash_repository from goods group by cat_id;

# How should we represent the total money?
select sum(goods_num * shop_price) from goods;
分享到:
评论

相关推荐

    mysql使用GROUP BY分组实现取前N条记录的方法

    本文实例讲述了mysql使用GROUP BY分组实现取前N条记录的方法。分享给大家供大家参考,具体如下: MySQL中GROUP BY分组取前N条记录实现 mysql分组,取记录 GROUP BY之后如何取每组的前两位下面我来讲述mysql中GROUP BY...

    MySQL数据库中group by语句与update语句的用法研究.pdf

    MySQL数据库中group by语句与update语句的用法研究.pdf

    深入解析mysql中order by与group by的顺序问题

    mysql 中order by 与group by的顺序是:selectfromwheregroup byorder by注意:group by 比order by先执行,order by不会对group by 内部进行排序,如果group by后只有一条记录,那么order by 将无效。要查出group ...

    Mysql利用group by分组排序

    我们可以利用MySQL中的group by的特性。 MySQL的group by与Oracle有所不同,查询得字段可以不用写聚合函数,查询结果取得是每一组的第一行记录。 利用上面的特点,可以利用mysql实现一种独特的排序; 首先先按某个...

    mysql获取group by总记录行数的方法

    本文实例讲述了mysql获取group by总记录行数的方法,分享给大家供大家参考。具体方法分析如下: 一般来说,mysql获取group by内部可以获取到某字段的记录分组统计总数,而无法统计出分组的记录数。 mysql中可以使用...

    mysql分组取每组前几条记录(排名) 附group by与order by的研究

    –按某一字段分组取最大(小)值所在行的数据 代码如下: /* 数据如下: nameval memo a 2 a2(a的第二个值) a 1 a1–a的第一个值 a 3 a3:a的第三个值 b 1 b1–b的第一个值 b 3 b3:b的第三个值 b 2 b2b2b2b2 b 4 b4b4 b ...

    mysql group by用法

    mysql group by用法:文章以图文并茂的方式详细介绍了mysql group by用法,包含语法和sql的使用、与where子句连用、与having子句连用等方式。

    深度分析mysql GROUP BY 与 ORDER BY

    本文就和大家一起深入研究下mysql中group by与order by.下面是我模拟我的内容表   我现在需要取出每个分类中最新的内容 select * from test group by category_id order by `date` 结果如下   明显。这不是我想...

    mysql获取group by的总记录行数另类方法

    mysql获取group by内部可以获取到某字段的记录分组统计总数,而无法统计出分组的记录数。 mysql的SQL_CALC_FOUND_ROWS 使用 获取查询的行数 在很多分页的程序中都这样写: 代码如下 SELECT COUNT(*) from `table` ...

    mysql筛选GROUP BY多个字段组合时的用法分享

    代码如下: group by fielda,fieldb,fieldc… 循环的时候可以通过判断后一个跟前面一个是否相同来分组,一个示例 代码如下: $result = mysql_query(“SELECT groups,name,goods FROM table GROUP BY groups,name ...

    MySQL优化GROUP BY方案

    满足GROUP BY子句的最一般的方法是扫描整个表并创建一个新的临时表,表中每个组的所有行应为连续的,然后使用该临时表来找到组并应用累积函数(如果有)。在某些情况中,MySQL能够做得更好,即通过索引访问而不用创建...

    MySQL无GROUP BY直接HAVING返回空的问题分析

    主要介绍了MySQL无GROUP BY直接HAVING返回空的问题分析,学习MYSQL需要注意这个问题

    mysql中order by与group by的区别

    order by 从英文里理解就是行的排序方式,默认的为升序。 order by 后面必须列出排序的字段名,可以是... 您可能感兴趣的文章:sql中 order by 和 group by的区别深度分析mysql GROUP BY 与 ORDER BYgroup by,having,o

    mysql不支持group by的解决方法小结

    下载安装的是最新版的mysql5.7.x版本,默认是开启了 only_full_group_by 模式的,但开启这个模式后,原先的 group by 语句就报错,然后又把它移除了

    基于mysql实现group by取各分组最新一条数据

    前言: group by函数后取到的是分组中的第一条数据,但是我们有时候需要取出各分组的最新一条,该怎么实现呢? 本文提供两种实现方式。 一、准备数据 ... 二、三种实现方式 1)先order by之后再分组: ...

    mysql中count(), group by, order by使用详解

    mysql中order by 排序查询、asc升序、desc降序,group by 分组查询、having 只能用于group by子句、作用于组内,having条件子句可以直接跟函数表达式。使用group by 子句的查询语句需要使用聚合函数。

    MySQL中无GROUP BY情况下直接使用HAVING语句的问题探究

    主要介绍了MySQL中无GROUP BY情况下直接使用HAVING语句的问题探究,同时探究了该情况下MAX与MIN功能的使用情况,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics