zoukankan      html  css  js  c++  java
  • mysql关联查询

    mysql数据库的统计------生成统计信息


    1.distinct:在一组之中将各个唯一的值找出来,如找出所有的品牌种类

    mysql>select distinct brand_kind from brand;
    +---------------+
    | brand_kind |
    +---------------+
    | A                |
    | B                |
    | C                |
    +---------------+

    2.计数统计,使用count(*),若没有带where子句,则将统计当前表的行数

    a.统计brand中的行数

    mysql> select count(*)from brand;
    +-----------+
    | count(*) |
    +-----------+
    | 5            |
    +-----------+

    b.统计一下brand表中brand_kind字段中品牌的数量
    mysql>select count(distinct brand_kind) as brandkind_count from brand;

    +----------------------+
    | brandkind_count |
    +-----------------------+
    | 3                          |
    +-----------------------+
    c.添加where子句的count(*)统计,统计出where子句筛选出来的数据的条数。
    统计出来trade表中订单时间为2017年10月的数据条数
    mysql> select count(*) from trade where trade_time=201710;
    +-----------+
    | count(*) |
    +-----------+
    | 6            |
    +-----------+
    d.区别count(*),count(字段名),count(distinct good_kind)区别。count(*)统计被查数据的行数,count(字段名)统计该字段非NULL的数据条数,count(distinct good_kind)统计出被查数据种类数量。

    mysql> select count(*), count(good_kind),count(distinct good_kind) from trade;
    +-----------+------------------+----------------------------------------+
    | count(*) | count(good_kind) | count(distinct good_kind) |
    +----------+------------------+-----------------------------------------+
    | 8           | 7                    | 3                                               |
    +----------+------------------+-----------------------------------------+

    e.根据group by 语句对字段进行分组,然后联合聚合函数对数据进行查询,统计操作。
    聚合函数
    max(),min(),sum(),count(),avg()

    内连接
    select *from brand,trade where brand.brand_id=trade.brandid;
    左外连接
    left outer join:返回包含左表中的所有的纪录和右表中连接字段相等
    right outer join:返回包含右表中的所有的纪录和左表中连接字段相等

    select distinct brand_kind, count(distinct userid) as user_num,count(distinct trade_id) as trade_num,sum(money) from brand left outer join trade on brand.brand_id=trade.brandid where trade_time=201710 group by brand_kind;

  • 相关阅读:
    AVLTree的实现以及左右旋转维持自平衡
    哈希函数之布隆过滤器
    LeetCode——线段树解决区间总和问题
    第23章 Windows身份验证
    第22章 使用外部身份提供商登录
    第21章 登录
    第20章 定义客户端
    第19章 定义资源
    第18章 启动
    第17章 社区快速入门和模板
  • 原文地址:https://www.cnblogs.com/kuangkuangduangduang/p/10248044.html
Copyright © 2011-2022 走看看