mysql count、sum、avg、min、max函数附带条件查询

您所在的位置:网站首页 SQL聚合函数max和min的参数可以是 mysql count、sum、avg、min、max函数附带条件查询

mysql count、sum、avg、min、max函数附带条件查询

2024-04-11 03:33| 来源: 网络整理| 查看: 265

一、count()

        1、不加条件求个数

                count(id)

        2、添加条件求个数

        count(status='0' or null)         count(status in ('1','6') or null)         count(status >1 or null)         count(DISTINCT ajh,(daml='WS' and lbh = 'LS') or null)

            

二、sum()

        1、不加条件查询

                sum(money)

        2、加条件查询

                sum(if(status='0',money,0))         

                sum(if(status in ('1','6'),money,0))         

                sum(if(status >1,money,0))

三、avg()

        1、不加条件查询

                avg(money)

        2、加条件查询

                avg(if(status='0',money,0))         

                avg(if(status in ('1','6'),money,0))         

                avg(if(status >1,money,0))

四、max()

        1、不加条件查询

                max(money)

        2、加条件查询

                max(if(status='0',money,0))         

                max(if(status in ('1','6'),money,0))         

                max(if(status >1,money,0))

五、min()

        1、不加条件查询

                min(money)

        2、加条件查询

                min(if(status='0',money,null))         

                min(if(status in ('1','6'),money,null))         

                min(if(status >1,money,null))



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3