mysql高级查询命令

一、普通查询

  整体遵循以下次序:

  

  1、常用聚合函数如下:

    

    eg1 : 找出表中的最大攻击力的值?

      select max(attack) from sanguo;

    eg2 : 表中共有多少个英雄?

      select count(name) as number from sanguo;

    eg3 : 蜀国英雄中攻击值大于200的英雄的数量

      select count(id) from sanguo where country='蜀国' and attack>200;

  2、group by :给查询的结果进行分组

     eg1 : 计算每个国家的平均攻击力

      select country,avg(attack) from sanguo group by country;

    eg2 : 所有国家的男英雄中 英雄数量最多的前2名的 国家名称及英雄数量

      select country,count(id) as number from sanguo where gender='M' group by country order by number DESC limit 2;

    注意:group by后字段名必须要为select后的字段,查询字段和group by后字段不一致,则必须对该字段进行聚合处理(聚合函数)

  3、having语句:对分组聚合后的结果进行进一步筛选

    eg1 : 找出平均攻击力大于105的国家的前2名,显示国家名称和平均攻击力

      select country,avg(attack) from sanguo group by country having avg(attack)>105 order by avg(attack) DESC limit 2;

    注意:

      1)having语句通常与group by联合使用

      2)having语句存在弥补了where关键字不能与聚合函数联合使用的不足,where只能操作表中实际存在的字段,having操作的是聚合函数生成的显示列

  4、distinct语句:不显示字段重复值

    eg1 : 表中都有哪些国家

       select distinct name,country from sanguo;

    eg2 : 计算一共有多少个国家

      select count(distinct country) from sanguo;

    注意:

      1)distinct和from之间所有字段都相同才会去重

      2)distinct不能对任何字段做聚合处理

  5、查询表记录时做数学运算

      运算符 : + - * / % **

    eg1: 查询时显示攻击力翻倍

      select name,attack*2 from sanguo;

    eg2: 更新蜀国所有英雄攻击力 * 2

      update sanguo set attack=attack*2 where country='蜀国';

二、嵌套查询(子查询)

  1、定义:把内层的查询结果作为外层的查询条件

  2、语法格式:

      select ... from 表名 where 条件(select ....);

  3、示例:

    1)把攻击值小于平均攻击值的英雄名字和攻击值显示出来

      select name,attack from sanguo where attack<(select avg(attack) from sanguo);

    2)找出每个国家攻击力最高的英雄的名字和攻击值(子查询)

      select name,attack from sanguo where (country,attack) in(select country,max(attack) from sanguo group by country);
三、
多表查询

  1、笛卡尔积
    select 字段名列表 from 表名列表;
  2、
多表查询

    select 字段名列表 from 表名列表 where 条件;
  3、
示例:

    sql脚本如下:
    
mysql -uroot -p123456
mysql>source /home/tarena/join_query.sql
create database if not exists db1 character set utf8;
use db1;

create table if not exists province(
id int primary key auto_increment,
pid int,
pname varchar(15)
)default charset=utf8;

insert into province values
(1, 130000, '河北省'),
(2, 140000, '陕西省'),
(3, 150000, '四川省'),
(4, 160000, '广东省'),
(5, 170000, '山东省'),
(6, 180000, '湖北省'),
(7, 190000, '河南省'),
(8, 200000, '海南省'),
(9, 200001, '云南省'),
(10,200002,'山西省');

create table if not exists city(
id int primary key auto_increment,
cid int,
cname varchar(15),
cp_id int
)default charset=utf8;

insert into city values
(1, 131100, '石家庄市', 130000),
(2, 131101, '沧州市', 130000),
(3, 131102, '廊坊市', 130000),
(4, 131103, '西安市', 140000),
(5, 131104, '成都市', 150000),
(6, 131105, '重庆市', 150000),
(7, 131106, '广州市', 160000),
(8, 131107, '济南市', 170000),
(9, 131108, '武汉市', 180000),
(10,131109, '郑州市', 190000),
(11,131110, '北京市', 320000),
(12,131111, '天津市', 320000),
(13,131112, '上海市', 320000),
(14,131113, '哈尔滨', 320001),
(15,131114, '雄安新区', 320002);

create table if not exists county(
id int primary key auto_increment,
coid int,
coname varchar(15),
copid int
)default charset=utf8;

insert into county values
(1, 132100, '正定县', 131100),
(2, 132102, '浦东新区', 131112),
(3, 132103, '武昌区', 131108),
(4, 132104, '哈哈', 131115),
(5, 132105, '安新县', 131114),
(6, 132106, '容城县', 131114),
(7, 132107, '雄县', 131114),
(8, 132108, '嘎嘎', 131115);

    1、显示省和市的详细信息

       河北省  石家庄市
       河北省  廊坊市
       湖北省  武汉市
       select province.pname,city.cname from province,city where province.pid=city.cp_id;

    2、显示 省 市 县 详细信息      select province.pname,city.cname,county.coname from province,city,county where province.pid=city.cp_id and city.cid=county.copid;

四、
连接查询

  1、内连接(结果同多表查询,显示匹配到的记录)
    
    语法格式:
      select 字段名 from 表1 inner join 表2 on 条件 inner join 表3 on 条件;
    eg1 : 显示省市详细信息
       select province.pname,city.cname from province inner join city on province.pid=city.cp_id;
    eg2 : 显示 省 市 县 详细信息
       select province.pname,city.cname,county.coname from province inner join city on province.pid=city.cp_id inner join county on city.cid=county.copid;
  2、
左外连接:以 左表 为主显示查询结果

    语法格式:
      select 字段名 from 表1 left join 表2 on 条件 left join 表3 on 条件;

      eg1 : 显示 省 市 详细信息(要求省全部显示)
        select province.pname,city.cname from province left join city on province.pid=city.cp_id;
  3、
右外连接:用法同左连接,以右表为主显示查询结果

    语法格式:
      select 字段名 from 表1 right join 表2 on 条件 right join 表3 on 条件;
 
 

    
 
 
 
 
 
 
 
 
 
 

    

猜你喜欢

转载自www.cnblogs.com/yuxiangyang/p/11079014.html