MySQL 查询

SQL查询

1、select ...聚合函数 from 表名
  1、where ...
  2、group by ...

  3、select ...聚合函数from表明
  4、having ...
  5、order by ...
  6、limit ...;

2、group by
  1、作用 :给查询结果进行分组
  2、示例
    1、查询表中一共有几个国家

       select country from sanguo group by country;

    2、计算每个国家的平均攻击力
      select country,avg(gongji) from sanguo
      group by country;
    先分组 -> 再聚合 -> 再去重
    蜀国
    蜀国
    蜀国 --> 120 --> 蜀国
    魏国
    魏国 --> 110 --> 魏国
    吴国 --> 115 --> 吴国
    3、查找所有国家中英雄数量最多的前2名的 国家名称和英雄数量(先分组再排序
      select country,count(id) as number from sanguo
      group by country
      order by number desc
      limit 2;
  3、注意
    1、group by之后的字段名必须要为select之后的字段名
    2、如果select之后的字段名和group by之后的字段不一致,则必须对该字段进行聚合处理(聚合函数)

3、having语句
  1、作用
    对查询的结果进行进一步筛选
  2、示例
    1、找出平均攻击力>105的国家的前2名,显示国家名和平均攻击力
      select country,avg(gongji) as pjgj from sanguo
      group by country
      having pjgj>105
      order by pjgj DESC
      limit 2;
  3、注意
    1、having语句通常和group by语句联合使用,过滤由group by语句返回的记录集
    2、where只能操作表中实际存在字段,having可操作由聚合函数生成的显示列

4、distinct
  1、作用 :不显示字段重复值
  2、示例
    1、表中都有哪些国家
      select distinct country from sanguo;
    2、计算蜀国一共有多少个英雄
      select count(distinct id) from sanguo
      where country="蜀国";
    3、注意
      1、distinct和from之间所有字段都相同才会去重
      2、distinct不能对任何字段做聚合处理

5、order by
  1、给查询结果进行排序
  2、... order by 字段名 ASC/DESC
  3、升序:ASC(默认)
   降序:DESC
  4、示例
    1、将英雄按防御值从高到低排序

    2、将蜀国英雄按攻击值从高到低排序

    3、将魏蜀两国英雄中名字为三个字的按防御值升序排列
      select * from sanguo
      where
      country in("蜀国","魏国") and name like "___"
      order by fangyu ASC;

      select * from sanguo
      where
      (country="魏国" or country="蜀国") and name like "___"
      order by fangyu;


6、limit (永远放在SQL语句的最后写)
  1、作用 :限制显示查询记录的个数
  2、用法
    1、limit n -> 显示 n 条记录
    2、limit m,n
      m 表示 从第m+1条记录开始显示,显示 n 条
      limit 2,3 : 第 3、4、5 三条记录
  3、示例
    1、在蜀国英雄中,查找防御值倒数第二名至倒数第四名的英雄的记录
    select * from sanguo
    where country="蜀国"
    order by fangyu asc
    limit 1,3;
    2、在蜀国英雄中,查找攻击值前3名且名字不为 NULL 的英雄的姓名、攻击值和国家
    select name,gongji,country from sanguo
    where
    country="蜀国" and name is not NULL
    order by gongji DESC
    limit 3;
  4、分页
    每页只能显示5条记录,显示第4页的内容

    第1页 :limit 0,5 # 1 2 3 4 5
    第2页 :limit (2-1)*5,5 # 6 7 8 9 10
    第3页 :limit (3-1)*5,5 # 11 12 13 14 15
    第4页 :limit (4-1)*5,5 # 16 17 18 19 20

    每页显示n条记录,显示第m页 :limit (m-1)*n,n

嵌套查询(子查询)

1、定义 :把内层的查询结果作为外层的查询条件
2、语法格式
  select ... from 表名 where 条件(select ....);
3、示例
1、把攻击值小于平均攻击值的英雄名字和攻击值显示出来
1、先计算平均值
  select avg(gongji) from MOSHOU.sanguo;
2、找到 < 平均值
  select name,gongji from MOSHOU.sanguo
  where gongji<平均值;
  子查询:
  select name,gongji from MOSHOU.sanguo
  where
  gongji<(select avg(gongji) from MOSHOU.sanguo);
2、找出每个国家攻击力最高的英雄的名字和攻击值
  select name,gongji from sanguo
  where
  gongji in(select max(gongji) from sanguo group by country);
  ## 无BUG
  select name,gongji from sanguo
  where
  (country,gongji) in
  (select country,max(gongji) from sanguo group by country);
  蜀国:100
  魏国:200
  吴国:300
  吴国:三个英雄 300 200 100

多表查询

1、两种方式
  1、select 字段名列表 from 表名列表; (笛卡尔积)
    t1 : name -> "A1" "A2" "A3"
    t2 : name -> "B1" "B2"
    select * from t1,t2;
    +------+-------+
    | name | name2 |
    +------+-------+
    | A1  | B1   |
    | A1  | B2   |
    | A2  | B1   |
    | A2  | B2   |
    | A3  | B1   |
    | A3  | B2   |
    +------+-------+
  2、... where 条件;
2、练习
1、显示省和市的详细信息
  河北省 石家庄市
  河北省 廊坊市
  湖北省 武汉市
  select sheng.s_name,city.c_name from sheng,city
  where
  sheng.s_id=city.cfather_id;
2、显示省市县详细信息
  select sheng.s_name as sheng,city.c_name as city,xian.x_name as xian from sheng,city,xian
  where
  sheng.s_id=city.cfather_id and
  city.c_id=xian.xfather_id;

连接查询

1、内连接
  1、语法格式
    select 字段名 from
    表1 inner join 表2 on 条件
    inner join 表3 on 条件;
  2、显示省市详细信息
    select sheng.s_name,city.c_name from sheng
    inner join city on sheng.s_id=city.cfather_id;
  3、显示省市县详细信息
    select sheng.s_name,city.c_name,xian.x_name from
    sheng inner join city
    on sheng.s_id=city.cfather_id
    inner join xian
    on city.c_id=xian.xfather_id;
2、外连接
  1、左连接
    1、以 左表 为主显示查询结果
    2、select 字段名 from
      表1 left join 表2 on 条件
      left join 表3 on 条件;
    3、显示省市详细信息
      select sheng.s_name,city.c_name from sheng
      left join city
      on sheng.s_id=city.cfather_id;
  2、右连接
    用法同左连接,以右表为主显示查询结果

 

posted @ 2018-07-31 22:57  凌逆战  阅读(208)  评论(0编辑  收藏  举报