zoukankan      html  css  js  c++  java
  • MySQL的聚集函数

    在使用过程中,有时不免用到求最大,最小,等聚集函数。今天记录一下

    函数 说明
    AVG() 返回某列的平均值
    COUNT() 返回某列的行数
    MAX()  返回某列的最大值
    MIN() 返回某列的最小值
    SUM()  返回某列值之和

    用到测试数据如下

    create table gather
    (
        name   varchar(20)   null,
        price  decimal(7, 3) null,
        remark varchar(200)  null
    )
        comment '聚集函数练习表';	
    INSERT INTO learn.gather (name, price, remark) VALUES ('apple', 2.000, '苹果说明');
    INSERT INTO learn.gather (name, price, remark) VALUES ('pear', 8.000, '223344@qq.com');
    INSERT INTO learn.gather (name, price, remark) VALUES ('banana', 3.000, '13612364589');
    INSERT INTO learn.gather (name, price, remark) VALUES ('Orange', 5.000, '12345.31');
    INSERT INTO learn.gather (name, price, remark) VALUES ('strawberry', 4.000, 'nothing');
    INSERT INTO learn.gather (name, price, remark) VALUES ('grapefruit', 2.000, '柚子');
    INSERT INTO learn.gather (name, price, remark) VALUES ('Watermelon', null, '西瓜');

    avg()

    该函数求的是一个列的平均值,且该函数会自动忽略列值为null的行。

    比如:

    select avg(price) as  avg_price from gather;

    count()

    该函数是对行的数目进行统计,该函数有两种用法。一种是count(*),会统计所有的行。还有一种是count(列名),统计该列列值不为null的行数。

    比如:

    select count(*) from gather;

    select count(price) from gather; 

    max()

    该函数会返回列中最大的值,但是会忽略列值为null的行。对于是数值型的会找出最大的那个。对于非数值型的max()函数会返回最后一行

    select max(price) from gather;

    select max(name) from gather;

    min()

    该函数会找出列的最小值,但是会忽略列值为null的行。如果列值为数值型时,则会返回列值为最小的行。如果为非数值型,则会排序返回最前面的行。

    select min(price) from gather;

    select min(name) from gather;

    sum()

    该函数会返回列值的总和,它也会忽略列值为null的行。如果此列的值都是非数值型,用此函数会返回0

    select sum(price) from gather;

    生于忧患,死于安乐
  • 相关阅读:
    JavaFx在macOS下的文字渲染Bug
    Java多接口同名方法的冲突
    旧技术的惯性
    一点思考(1)
    slisp:编译到JVM平台上的lisp方言
    Arcee:又一个 Parser Generator 轮子
    使用Java实现一门简单的动态语言
    Hello World!
    [NOI2008]奥运物流
    [IOI2005]Riv河流
  • 原文地址:https://www.cnblogs.com/songlove/p/15542143.html
Copyright © 2011-2022 走看看