第八节:MySQL之Select指令详解和相关练习

一. 前言

该篇文章基于之前   https://www.cnblogs.com/yaopengfei/p/7182230.html  的基础上进行补充修改。

1. 简介

  就查询而言,可以简单的分为:单表查询 和 多表查询。

  单表查询包括:简单查询、过滤查询、结果排序、分页查询、聚集函数。

  多表查询包括:笛卡尔积、外键约束、内连接查询、外链接查询、自连接查询。

2. 数据准备

(1). 用到的表:

   产品表(product)。包括:主键id、产品名称(productName)、分类编号(dir_id)、零售价(salePrice)、供应商(supplier)、品牌(brand)、折扣(cutoff)、成本价(costPrice)。

   产品分类编号表( productdir)。 包括:主键id、编号名称( dirName)、父id( parent_id) 。

   产品库存表( productstock)。包括:主键id、产品id( product_id )、库存数量( storeNum)、上次进库时间(lastIncomeDate)、上次出库时间( lastOutcomeDate)、预警数量(warningNum)。

(2). 对应的字段类型,如下图:

(3). 用到的SQL语句: 

product表

CREATE TABLE `product` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `productName` varchar(50) DEFAULT NULL,
  `dir_id` bigint(11) DEFAULT NULL,
  `salePrice` double(10,2) DEFAULT NULL,
  `supplier` varchar(50) DEFAULT NULL,
  `brand` varchar(50) DEFAULT NULL,
  `cutoff` double(2,2) DEFAULT NULL,
  `costPrice` double(10,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES ('1', '罗技M90', '3', '90.00', '罗技', '罗技', '0.50', '35.00');
INSERT INTO `product` VALUES ('2', '罗技M100', '3', '49.00', '罗技', '罗技', '0.90', '33.00');
INSERT INTO `product` VALUES ('3', '罗技M115', '3', '99.00', '罗技', '罗技', '0.60', '38.00');
INSERT INTO `product` VALUES ('4', '罗技M125', '3', '80.00', '罗技', '罗技', '0.90', '39.00');
INSERT INTO `product` VALUES ('5', '罗技木星轨迹球', '3', '182.00', '罗技', '罗技', '0.80', '80.00');
INSERT INTO `product` VALUES ('6', '罗技火星轨迹球', '3', '349.00', '罗技', '罗技', '0.87', '290.00');
INSERT INTO `product` VALUES ('7', '罗技G9X', '3', '680.00', '罗技', '罗技', '0.70', '470.00');
INSERT INTO `product` VALUES ('8', '罗技M215', '2', '89.00', '罗技', '罗技', '0.79', '30.00');
INSERT INTO `product` VALUES ('9', '罗技M305', '2', '119.00', '罗技', '罗技', '0.82', '48.00');
INSERT INTO `product` VALUES ('10', '罗技M310', '2', '135.00', '罗技', '罗技', '0.92', '69.80');
INSERT INTO `product` VALUES ('11', '罗技M505', '2', '148.00', '罗技', '罗技', '0.92', '72.00');
INSERT INTO `product` VALUES ('12', '罗技M555', '2', '275.00', '罗技', '罗技', '0.88', '140.00');
INSERT INTO `product` VALUES ('13', '罗技M905', '2', '458.00', '罗技', '罗技', '0.88', '270.00');
INSERT INTO `product` VALUES ('14', '罗技MX1100', '2', '551.00', '罗技', '罗技', '0.76', '300.00');
INSERT INTO `product` VALUES ('15', '罗技M950', '2', '678.00', '罗技', '罗技', '0.78', '320.00');
INSERT INTO `product` VALUES ('16', '罗技MX Air', '2', '1299.00', '罗技', '罗技', '0.72', '400.00');
INSERT INTO `product` VALUES ('17', '罗技G1', '4', '155.00', '罗技', '罗技', '0.80', '49.00');
INSERT INTO `product` VALUES ('18', '罗技G3', '4', '229.00', '罗技', '罗技', '0.77', '96.00');
INSERT INTO `product` VALUES ('19', '罗技G500', '4', '399.00', '罗技', '罗技', '0.88', '130.00');
INSERT INTO `product` VALUES ('20', '罗技G700', '4', '699.00', '罗技', '罗技', '0.79', '278.00');
View Code

productdir表

CREATE TABLE `productdir` (
  `id` bigint(11) NOT NULL auto_increment,
  `dirName` varchar(30) default NULL,
  `parent_id` bigint(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `productdir` VALUES ('1', '鼠标', null);
INSERT INTO `productdir` VALUES ('2', '无线鼠标', '1');
INSERT INTO `productdir` VALUES ('3', '有线鼠标', '1');
INSERT INTO `productdir` VALUES ('4', '游戏鼠标', '1');
View Code

productstock表

CREATE TABLE `productstock` (
  `id` bigint(11) NOT NULL auto_increment,
  `product_id` bigint(11) default NULL,
  `storeNum` int(10) default NULL,
  `lastIncomeDate` datetime default NULL,
  `lastOutcomeDate` datetime default NULL,
  `warningNum` int(10) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `productstock` VALUES ('1', '1', '182', '2015-03-12 20:33:00', '2015-03-12 20:33:04', '20');
INSERT INTO `productstock` VALUES ('2', '2', '27', '2015-03-02 20:33:28', '2015-03-09 20:33:40', '20');
INSERT INTO `productstock` VALUES ('3', '3', '89', '2015-02-28 20:34:13', '2015-03-12 20:34:19', '20');
INSERT INTO `productstock` VALUES ('4', '5', '19', '2015-03-01 20:34:43', '2015-03-12 20:34:48', '20');
INSERT INTO `productstock` VALUES ('5', '6', '3', '2015-02-01 20:35:12', '2015-03-02 20:35:16', '5');
INSERT INTO `productstock` VALUES ('6', '7', '2', '2015-02-02 20:35:59', '2015-02-27 20:36:05', '3');
INSERT INTO `productstock` VALUES ('7', '8', '120', '2015-03-12 20:36:31', '2015-03-12 20:36:33', '20');
INSERT INTO `productstock` VALUES ('8', '9', '58', '2015-03-02 20:36:50', '2015-03-12 20:36:53', '20');
INSERT INTO `productstock` VALUES ('9', '11', '28', '2015-03-02 20:37:12', '2015-03-12 20:37:15', '20');
INSERT INTO `productstock` VALUES ('10', '12', '8', '2015-03-02 20:37:35', '2015-03-09 20:37:38', '5');
INSERT INTO `productstock` VALUES ('11', '13', '3', '2015-03-02 20:37:58', '2015-03-12 20:38:01', '5');
INSERT INTO `productstock` VALUES ('12', '14', '6', '2015-03-02 20:38:20', '2015-03-07 20:38:23', '5');
INSERT INTO `productstock` VALUES ('13', '15', '2', '2015-02-02 20:38:38', '2015-02-24 20:38:44', '5');
INSERT INTO `productstock` VALUES ('14', '16', '3', '2015-02-02 20:39:05', '2015-02-06 20:39:09', '3');
INSERT INTO `productstock` VALUES ('15', '17', '49', '2015-03-02 20:39:36', '2015-03-12 20:39:40', '20');
INSERT INTO `productstock` VALUES ('16', '18', '14', '2015-03-02 20:39:57', '2015-03-09 20:40:01', '10');
INSERT INTO `productstock` VALUES ('17', '20', '7', '2015-03-02 20:40:22', '2015-03-03 20:40:25', '5');
View Code

 3. 经典SQL的执行顺序

二. 单表查询

 1. 简单查询

    a.  消除结果中的重复数据,用 DISTINCT 关键字。

     b.  数学逻辑运算符(+ - * / ),运算符的优先级为:

         (1). 乘除高于加减。

         (2). 同级运算从左到右。

         (3). 括号的优先级最高。

    c.  设置列名的别名。

         (1). 改变列的标题头,用 as 关键字,可以省略。

         (2). 用于表示计算结果的含义。

         (3). 如果别名中有特殊字符,或者轻质大小写敏感,或有空格是,都需要加双引号

#1.需求:查询所有货品信息
select * from product

#2.查询所有货品的id,productName,salePrice
select id,productName,salePrice from product

#3 需求:查询商品的分类编号。
SELECT DISTINCT dir_id FROM product
#需求:查询所有货品的id,名称和批发价(批发价=卖价*折扣)
SELECT id,productName,salePrice*cutoff FROM product

#需求:查询所有货品的id,名称,和各进50个的成本价(成本=costPirce)
SELECT id,productName,costPrice*50 FROM product

#需求:查询所有货品的id,名称,各进50个,并且每个运费1元的成本
SELECT id,productName,(costPrice+1)*50 FROM product
SELECT id,productName,(costPrice+1)*50 as pf FROM product

SELECT id,productName,(costPrice+1)*50  pf FROM product

SELECT id,productName,(costPrice+1)*50  "p f" FROM product

2. 过滤查询

   a. SQL语句的执行顺序: FROM → WHERE → SELECT → ORDER BY。

   b. 逻辑运算符:

       (1). AND (&&) : 组合条件都为true,返回true。

       (2). OR ( || ) : 组合条件之一为true, 就返回true。

       (3). NOT ( ! ) : 组合条件都为false,返回true。

需求: 选择id,货品名称,批发价在300-400之间的货品
SELECT id productName,salePrice FROM product WHERE salePrice >=300 AND salePrice<=400

需求: 选择id,货品名称,分类编号为2,4的所有货品
SELECT id productName,salePrice FROM product WHERE  dir_id=3 OR dir_id<=4

需求: 选择id,货品名词,分类编号不为2的所有商品
SELECT id productName,salePrice FROM product WHERE  dir_id!=2
SELECT id productName,salePrice FROM product WHERE  NOT dir_id=2

需求: 选择id,货品名称,分类编号的货品零售价大于等于250或者是成本大于等于200
SELECT id,productName,dir_id,salePrice,costPrice from product WHERE salePrice >=250 or costPrice>=200

  c. 比较运算符:

       (1). 等于: =

       (2). 大于: >

       (3). 大于或等于: >=

       (4). 小于:<

       (5). 小于或等于:<=

       (6). 不等于: !=    或 <>

     注意:运算符的优先级由低到高:所有比较运算符 → NOT → AND → OR → 括号。

# 需求: 查询货品零售价为119的所有货品信息.
SELECT * FROM product WHERE salePrice=119

#需求: 查询货品名为罗技G9X的所有货品信息.
SELECT * FROM product WHERE productName='罗技G9X'
SELECT * FROM product WHERE productName='罗技g9x'
SELECT * FROM product WHERE BINARY productName='罗技g9x'   #二进制区分大小写

#需求: 查询货品名 不为 罗技G9X的所有货品信息.
SELECT * FROM product WHERE productName!='罗技G9X'
SELECT * FROM product WHERE productName<>'罗技G9X'

需求: 查询分类编号不等于2的货品信息
SELECT * FROM product WHERE dir_id!=2

需求: 查询货品名称,零售价小于等于200的货品
SELECT productName FROM product WHERE salePrice<=200

需求: 查询id,货品名称,批发价大于350的货品
SELECT id,productName,salePrice *cutoff FROM product WHERE salePrice *cutoff >350

思考:使用where后面使用别名不行,总结select和where的执行顺序(思考下面两个都不好用的原因,
第二个是因为别名只能放在列名的后面)
SELECT id,productName,salePrice *cutoff pf FROM product WHERE pf >350
SELECT id,productName, pf FROM product WHERE salePrice *cutoff pf >350
SELECT id,productName FROM product WHERE (NOT productName LIKE '%M%'  AND salePrice > 100) OR (dir_id = 2)

      d. 范围查询:BETWEEN AND 表示某一值域范围的记录。

        格式: SELECT * FROM 表名 WHERE 列名 BETWEEN minvalue AND maxvalue;   (两边都为闭区间)。

需求: 选择id,货品名称,批发价在300-400之间的货品
SELECT id,productName,salePrice FROM product WHERE salePrice BETWEEN 300 AND 400

需求: 选择id,货品名称,批发价不在300-400之间的货品

SELECT id,productName,salePrice FROM product WHERE NOT salePrice BETWEEN 300 AND 400

     e. 集合查询:使用IN运算符,判断列的值是否在指定的集合中。

        格式: WHERE 列名 IN ( 值1, 值2,....) 。

需求:选择id,货品名称,分类编号为2,4的所有货品
SELECT id productName,salePrice FROM product WHERE  dir_id=2 OR dir_id=4
SELECT id productName,salePrice FROM product WHERE  dir_id IN(2,4)

  需求:选择id,货品名称,分类编号不为2,4的所有货品
  SELECT id productName,salePrice FROM product WHERE NOT dir_id IN(2,4)

     f. 空值判断:IS NULL,判断列的值是否为空。

         格式:WHERE 列名 IS NULL。

--需求:查询商品名为NULL的所有商品信息。
SELECT * FROM product WHERE productName IS NULL

     g. 模糊查询:使用 LIKE 运算符执行通配查询。

         (1). %: 表示零或多个字符。

         (2). _ : 表示一个字符。

需求: 查询id,货品名称,货品名称匹配'%罗技M9_'
SELECT id, productName FROM product WHERE productName LIKE '%罗技M9_'

需求: 查询id,货品名称,分类编号,零售价大于等于200并且货品名称匹配'%罗技M1__'
SELECT id,productName,dir_id FROM product WHERE productName LIKE '%罗技M9__' AND salePrice >=200

3. 结果排序

   使用ORDER BY 子句进行排序,ASC: 升序 (默认,可省),DESC:降序;ORDER BY 子句出现在SELECT语句最后。

   格式: SELECT * 

              FROM table_name

              WHERE 条件

              ORDER BY 列名1 [ASC/DESC] , 列名2 [ASC/DESC]  .... 。

注意: 不能对使用了引号的别名进行排序。

#按照某一列来排序:
#需求:选择id,货品名称,分类编号,零售价并且按零售价降序排序
SELECT id,productName,dir_id,salePrice from product ORDER BY salePrice DESC

#按多列排序:
#需求: 选择id,货品名称,分类编号,零售价先按分类编号排序,再按零售价排序
SELECT id,productName,dir_id,salePrice from product ORDER BY dir_id ASC,salePrice DESC

#列的别名排序:
#需求:查询M系列并按照批发价排序(加上别名)

SELECT * ,salePrice * cutoff  FROM product WHERE productName LIKE '%M%'  ORDER BY salePrice * cutoff
SELECT * ,salePrice * cutoff pf  FROM product WHERE productName LIKE '%M%'  ORDER BY pf

#需求:查询分类为2并按照批发价排序(加上别名)
SELECT * ,salePrice * cutoff  "pf" FROM product WHERE dir_id=2   ORDER BY "pf"

4. 分页

(1). 真分页(物理分页、数据库分页):每次分页的时候,都从数据库中截取指定条数的数据。优点:不会内存溢出。缺点:复杂,翻页比较慢

      假分页(逻辑分页、内存分页):一次性把数据全部查出来,存在内存里,翻页的时候,从内存中截取指定条数即可。优点:简单,翻页快。缺点:若数据过多,可能内存溢出

(2). MySQL中分页的写法:

   格式1:LIMIT beginIndex, pageSize ;

   其中,beginIndex,表示从哪一个索引位置开始截取数据(索引是从0开始),   pageSize,表示每页显示最多的条数

   分页语句:SELECT * FROM 表名 LIMIT (currentPage-1)*pageSize,pageSize .

   currentPage为当前页数,通常是前端传递过来的。

-- 分页 (获取的是 第11,12,13 条数据)
SELECT * FROM product LIMIT 10,3

   格式2:LIMIT N ;   代表取前N条数据。 (等价于: LIMIT 0,N )

--获取的是前10条数据
SELECT * FROM product LIMIT 10

   格式3:LIMIT M,-1 ;   代表获取第 M+1条 到最后的数据  ( MySQL5.7中已经不支持这种写法了)

--获取第11条 到 最后的数据
SELECT * FROM product LIMIT 10,-1

   格式4:LIMIT M OFFSET N ;   代表跨过N条数据,取M条数据

-- 获取的是第 3,4 ,5条数据
select * from T_WxLoginUser LIMIT 2,3;
-- 获取的也是第 3,4 ,5条数据  (和上面的语句效果等价)
select * from T_WxLoginUser LIMIT 3 OFFSET 2;

(3). SQLServer分页:

方案一:利用ROW_NUMBER() over() between  and,进行分页。ROW_NUMBER() over() 表示把该表按照某个字段进行排序,然后新生成一列,从1到n,如下图:

  over里的排序要晚于外层 where,group by,order by

  会按照over里面的排序,新增一列(1----n),比如 newRow, 然后基于这一列,使用between and 进行区间获取

  可以将出来的数据进行排列1-----n,即多了一列

select *, ROW_NUMBER() over(order by userAge desc) as newRow from UserInfor

 演变过程:

select *, ROW_NUMBER() over(order by userAge desc) as newRow from UserInfor

select * from 
(select *, ROW_NUMBER() over(order by userAge desc) as newRow from UserInfor) as t

select * from 
(select *, ROW_NUMBER() over(order by userAge desc) as newRow from UserInfor) as t
where t.newRow between 1 and 2 

方案2: 利用offset-fetch (SQLServer2012后开始支持),分页实现的思路:

 --在分页实现中,使用Order By子句,按照指定的columns对结果集进行排序;

 --使用offset子句跳过前N条:offset (@PageIndex-1)*@RowsPerPage rows;

 --使用fetch子句取N条:fetch next @RowsPerPage rows only;

--跨过3行取剩下的
select * from UserInfor order by userAge desc
offset 3 rows

--跨过3行取剩下2行
select * from UserInfor order by userAge desc
offset 3 rows
fetch next 2 rows only

封装成存储过程:(这里基于方案一、方案二各一种,然后基于方案一写了一个万能的分页)

-- 基于"方案一"的存储过程分页
if (exists (select * from sys.objects where name = 'FenYe1'))
    drop proc FenYe1
go
create proc FenYe1(
        @pageSize int=3,   --输入参数:每页的条数,默认值为2
        @pageIndex int=1,  --输入参数:当前页数,默认值为1
        @totalCount int output, --输出参数:总条数
        @pageCount int output   --输出参数:总页数
    )
as
    select * from 
   (select *, ROW_NUMBER() over(order by userAge desc) as newRow from UserInfor) as t
    where t.newRow between ((@pageIndex-1)*@pageSize)+1 and (@pageSize*@pageIndex);
    select @totalCount=COUNT(*) from UserInfor;
    set @pageCount=CEILING(@totalCount * 1.0 /@pageSize);

--执行该分页的存储过程
declare @myTotalCount int,   --声明变量用来接收存储过程中的输出参数
        @myPageCount int     --声明变量用来接收存储过程中的输出参数
exec FenYe1 2,1,@myTotalCount output,@myPageCount output;    --每页2条,求第1页的数据
select @myTotalCount as '总条数',@myPageCount as '总页数';



-- 基于"方案二"的存储过程分页
if (exists (select * from sys.objects where name = 'FenYe2'))
    drop proc FenYe2
go
create proc FenYe2(
        @pageSize int=3,   --输入参数:每页的条数,默认值为2
        @pageIndex int=1,  --输入参数:当前页数,默认值为1
        @totalCount int output, --输出参数:总条数
        @pageCount int output   --输出参数:总页数
    )
as
    select * from UserInfor order by userAge desc 
    offset (@pageIndex-1)*@pageSize rows fetch next @pageSize rows only;
    select @totalCount=COUNT(*) from UserInfor;
    set @pageCount=CEILING(@totalCount * 1.0 /@pageSize);

--执行该分页的存储过程
declare @myTotalCount int,   --声明变量用来接收存储过程中的输出参数
        @myPageCount int     --声明变量用来接收存储过程中的输出参数
exec FenYe2 4,2,@myTotalCount output,@myPageCount output;  --每页4条,求第2页的数据
select @myTotalCount as '总条数',@myPageCount as '总页数';


--基于"方案一"创建一个万能表的分页
if (exists (select * from sys.objects where name = 'WangNengFenYe'))
    drop proc WangNengFenYe
go
create proc WangNengFenYe(
    @TableName varchar(50),            --表名
    @ReFieldsStr varchar(200) = '*',   --字段名(全部字段为*)
    @OrderString varchar(200),         --排序字段(必须!支持多字段不用加order by)
    @WhereString varchar(500) =N'',    --条件语句(不用加where)
    @PageSize int,                     --每页多少条记录
    @PageIndex int = 1 ,               --指定当前为第几页
    @TotalRecord int output            --返回总记录数
)
as
begin
     --处理开始点和结束点
    Declare @StartRecord int;
    Declare @EndRecord int; 
    Declare @TotalCountSql nvarchar(500); 
    Declare @SqlString nvarchar(2000);    
    set @StartRecord = (@PageIndex-1)*@PageSize + 1
    set @EndRecord = @StartRecord + @PageSize - 1 
    SET @TotalCountSql= N'select @TotalRecord = count(*) from ' + @TableName;--总记录数语句
    SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句
    --
    IF (@WhereString! = '' or @WhereString!=null)
        BEGIN
            SET @TotalCountSql=@TotalCountSql + '  where '+ @WhereString;
            SET @SqlString =@SqlString+ '  where '+ @WhereString;            
        END
    --第一次执行得到
    --IF(@TotalRecord is null)
    --   BEGIN
           EXEC sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output;--返回总记录数
    --  END
    ----执行主语句
    set @SqlString ='select * from ' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' +  ltrim(str(@EndRecord));
    Exec(@SqlString)    
END

--执行
--对UserInfor表进行分页,根据userAge排序,每页4条,求第2页的数据

declare @totalCount int
exec WangNengFenYe 'UserInfor','*','userAge desc','',4,2,@totalCount output; 
select @totalCount as '总条数';--总记录数。
View Code

5. 聚集函数

   (1). COUNT : 统计结果的记录数。

   (2). MAX : 统计计算最大值。

   (3). MIN : 统计最小值。

   (4). SUM: 统计计算求和。

   (5). AVG: 统计计算平均值。

需求:查询所有商品平均零售价
SELECT AVG(salePrice) FROM product

需求:查询商品总记录数(注意在Java中必须使用long接收)
SELECT COUNT(id) FROM product

需求:查询分类为2的商品总数
SELECT COUNT(id) FROM product WHERE dir_id=2

需求:查询商品的最小零售价,最高零售价,以及所有商品零售价总和
SELECT MIN(salePrice) 最小零售价,MAX(salePrice) 最高零售价,SUM(salePrice) 商品零售价总和 FROM product

三. 多表查询

1. 笛卡尔积

  多表查询会产生笛卡尔积。 假设集合A={a,b},集合B={0,1,2},则两个集合的笛卡尔积为{(a,0),(a,1),(a,2),(b,0),(b,1),(b,2)},实际运行环境下,应避免使用全笛卡尔集。

  解决笛卡尔积最有效的方法:等值连接(是内连接的一种)。

       

-- 需求:查询所有的货品信息+对应的货品分类信息
SELECT productName,dirName FROM product,productdir WHERE dir_id = productdir.id

2. 外键约束

(1). 主键约束(PRIMARY KEY): 约束在当前表中,指定列的值非空且唯一.

(2). 外键约束(FOREIGN KEY): A表中的外键列. A表中的外键列的值必须参照于B表中的某一列(B表主键).

注意:在MySQL中,InnoDB支持事务和外键.修改表的存储引擎为InnDB。格式:ALTER TABLE 表名 ENGINE='InnoDB'。

        

3. 说明

(1) 主表:数据可以独立存在,就是被参考的表。 productdir

(2). 从表:表中的数据,必须参照于主表的数据。product

注意:在删除表的时候,先删除从表,再删除主表。

      

3. 多表查询详解

  多表查询包括三类:内连接查询(隐式内连接和显示内连接)、外连接查询 (左外链接、右外链接、全链接 )、自连接查询,各自的关系如下图:

     

经典代码分享:

/* 1 */
SELECT <select_list> FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key;
/* 2 */
SELECT <select_list> FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key;
/* 3 */
SELECT <select_list> FROM TableA A INNER JOIN TableB B ON A.Key = B.Key;
/* 4 */
SELECT <select_list> FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key WHERE
B.Key IS NULL;
/* 5 */
SELECT <select_list> FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key WHERE
A.Key IS NULL;
/* 6 */
SELECT <select_list> FROM TableA A FULL OUTER JOIN TableB B ON A.Key = B.Key;
/* MySQL不支持FULL OUTER JOIN这种语法 可以改成 1+2 */
SELECT <select_list> FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key
UNION
SELECT <select_list> FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key;
/* 7 */
SELECT <select_list> FROM TableA A FULL OUTER JOIN TableB B ON A.Key = B.Key
WHERE A.Key IS NULL OR B.Key IS NULL;
/* MySQL不支持FULL OUTER JOIN这种语法 可以改成 4+5 */
SELECT <select_list> FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key WHERE
B.Key IS NULL;
UNION
SELECT <select_list> FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key WHERE
A.Key IS NULL;
View Code

注:在MySQL中,join、inner join、cross join含义相同,都是用于等值连接的,另外MySQL中没有 full outer join,可以通过left join + right join +union来实现相应需求。

(1). 内连接

  内连接查询出来的结果是多表交叉共有的,分为:隐式内连接和显示内连接。 还有一种划分方式,内连接分为:等值连接和非等值连接。

PS:不管是隐式内连接还是显示内连接, 当写法是  A.列 = B.列 的时候,就是等值连接;但如果写成  A.列!=B.列 就是非等值连接,所以说等值连接是内连接的子级。【如有出入,欢迎探讨】

A. 隐式内连接 (关键字 =)

B. 显示内连接 (关键字  inner join on,inner可以省略,推荐写法

 PS 在做等值连接的时候,若A表中的和B表中的列相同,可以缩写为:(MySQL特有)

       

       

需求:查询所有商品的名称和分类名称:
隐式内连接: 
SELECT p.productName,pd.dirName FROM product p,productdir pd WHERE p.dir_id = pd.id
显示内连接: 
SELECT p.productName,pd.dirName FROM product p INNER JOIN productdir pd ON p.dir_id = pd.id
显示内连接:
 SELECT p.productName,pd.dirName FROM product p JOIN productdir pd ON p.dir_id = pd.id


需求: 查询零售价大于200的无线鼠标
SELECT * FROM product p,productdir pd WHERE p.dir_id = pd.id AND p.salePrice >200 And 
pd.dirName ='无线鼠标'

SELECT * FROM product p JOIN productdir pd on p.dir_id = pd.id WHERE p.salePrice >200 And 
pd.dirName ='无线鼠标'

需求: 查询每个货品对应的分类以及对应的库存
SELECT p.productName,pd.dirName,ps.storeNum 
FROM product p,productdir pd,productstock ps 
WHERE  p.dir_id = pd.id AND p.id = ps.product_id

SELECT p.productName,pd.dirName,ps.storeNum 
FROM product p 
JOIN productdir pd on  p.dir_id = pd.id
JOIN productstock ps on p.id = ps.product_id

需求: 如果库存货品都销售完成,按照利润从高到低查询货品名称,零售价,货品分类(三张表).
select *, (p.salePrice - p.costPrice) * ps.storeNum lirun
FROM product p,productdir pd,productstock ps 
WHERE  p.dir_id = pd.id AND p.id = ps.product_id
ORDER BY lirun DESC

select *, (p.salePrice - p.costPrice) * ps.storeNum lirun
FROM product p 
JOIN productdir pd on  p.dir_id = pd.id
JOIN productstock ps on p.id = ps.product_id
ORDER BY lirun DESC

(2). 外连接查询

 左外连接:查询出JOIN左边表的全部数据,JOIN右边的表不匹配的数据用NULL来填充,关键字:left join

 右外连接:查询出JOIN右边表的全部数据,JOIN左边的表不匹配的数据用NULL来填充,关键字:right join

 全连接: (左连接 - 内连接) +内连接 + (右连接 - 内连接) = 左连接+右连接-内连接  关键字:full join

PS: A LEFT JOIN B  等价于  B RIGHT JOIN A

注意:无论是左外连接还是右外连接,都要注意 1对多 的情况,左外链接的数据数量取决于左表,右外链接的数据数量取决于右表。

      

      

-- 外链接
# 查询所有商品的名称和分类名称
左连接:
SELECT * FROM product p LEFT JOIN productdir pd ON p.dir_id = pd.id
-- 等价于
SELECT * FROM  productdir pd RIGHT JOIN product p ON p.dir_id = pd.id
右连接:
SELECT * FROM product p RIGHT JOIN productdir pd ON p.dir_id = pd.id

 (3). 自连接查询:把一张表看成两张表来做查询

       

补充一个例子:

 S表(学生表):sno(学号)、sname(学生姓名)、sage、ssex。

 求学号比 WANG 同学大,而年龄比他小的学生姓名。

--写法1
select sname from S where 
sno > (select sno from S where sname = 'WANG') 
and 
sage < (select sage from S where sname ='WANG');

--写法2  (自连接)
select S2.sname from S as S1, S as S2
where S1.sno= S2.sno
and S1.sname='WANG' and S2.sno>S1.sno and S2.sage < S1.sage;

四. 其它 

 1. group by 分组

 SQL中的group by分组与linq、lambda完全不同,select中查询的字段必须是group by语句的后面字段,作为分组的依据;如果要使用其他字段,其他字段必须被包含在聚合函数中。常见的聚合函数有:count、sum、avg、min、max。

 group by 后面通常会加having,表示对分组后的数据进行限制。

案例1

--根据用户性别分类,统计不同性别年龄最大值,年龄总和,用户数量。
select userSex,MAX(userAge) as MaxAge,SUM(userAge) as TotalAges,count(*) as Num
from Sys_UserInfor
group by userSex

 案例2

  查询只选修了一门课程的学员姓名和年龄 

--S表(学生表):sno(学号)、sname(学生姓名)、sage、ssex
--C表(课程表):cno(课程号)、cname(课程名)、teacher(老师名)
--SC表(成绩表):sno、cno、grade(成绩)

select sname,sage from S 
where sno in
(select S.sno
from S inner join Sc on S.sno =Sc.sno
group by Sc.sno having count(*)>1);

案例3

 要求输出每个用户的基本信息、所在的群总数、自己创建的群数、参与别人创建的群数,没有的话数量显示0。

用到的表:

select t1.userPhone,t1.userNickName,t1.userType,TotalNum=ISNULL(t5.TotalNum,0) ,num1=ISNULL(t3.num1,0), num2=(ISNULL(t5.TotalNum,0)-ISNULL(t3.num1,0))
from T_ChatUser as t1
left join 
(
    select  t2.groupOwnerId as groupOwnerId , count(*) as num1 from  T_ChatGroup as t2 
    group by t2.groupOwnerId
) as t3
on t1.id =t3.groupOwnerId
left join
(
    select t4.userId, count(*) as TotalNum  from T_GroupUser as t4
    group by t4.userId
) as t5
on t1.id =t5.userId
order by t1.addTime desc

五. 实战练习

1. 练习1

--相关表说明

 --S表(学生表):sno(学号)、sname(学生姓名)、sage、ssex

 --C表(课程表):cno(课程号)、cname(课程名)、teacher(老师名)

 --SC表(成绩表):sno、cno、grade(成绩)

练习题分享:(待补充,修改)

--相关表说明
--S表(学生表):sno(学号)、sname(学生姓名)、sage、ssex
--C表(课程表):cno(课程号)、cname(课程名)、teacher(老师名)
--SC表(成绩表):sno、cno、grade(成绩)


--一. 测试一
--1. 求年龄大于所有女同学年龄的男学生姓名和年龄
select sname,sage from S where S.ssex='' and S.sage >(select Max(sage) from S where ssex='' );
select sname,sage from S where S.ssex='' and S.sage > All(select sage from S where ssex='');

--2.求年龄大于女同学平均年龄的男学生姓名和年龄。
select sname,sage from S where ssex='' and sage >(select Avg(sage) from S where S.ssex='');

--3.求成绩为空值的学生学号和课程号
select sno,cno from Sc where Sc.grade is null;

--4.检索姓名以 WANG 打头的所有学生的姓名和年龄。
select sname,sage from S where sname like 'WANG%';

--5.检索学号比 WANG 同学大,而年龄比他小的学生姓名。
select sname from S where 
sno > (select sno from S where sname = 'WANG') 
and 
sage < (select sage from S where sname ='WANG');

--写法2
select S2.sname from S as S1, S as S2
where S1.sno= S2.sno
and S1.sname='WANG' and S2.sno>S1.sno and S2.sage < S1.sage;


--6.统计每门课程的学生选修人数(超过 2 人的课程才统计),要求输出课程号、选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。
select cno,count(*) as Num from Sc
group by cno having count(*) >2
order by Num desc,cno asc;

--7.求 王森莉 老师所授课程的每门课程的学生平均成绩。
--显示内连接 (关键字  inner join on,inner可以省略,推荐写法) 
select Sc.cno,Avg(Sc.grade)
from Sc JOIN C on Sc.cno = C.cno
where C.teacher='王森莉'
group by cno ;

--隐式内连接 
select Sc.cno,Avg(Sc.grade)
from Sc , C 
where  Sc.cno = C.cno and C.teacher='王森莉'
group by cno ;

--8.求选修 数学 课程的学生的平均年龄。
select Avg(sage) from S
where S.sno in 
(select Sc.sno from Sc join C on Sc.cno=C.cno where C.cname='数学')


--9.统计有学生选修的课程门数。
select count(distinct cno) from Sc;


--测试二
--1. 查询选修课程名称为 数学 的学员学号和姓名
select sno,sname from S 
where sno in 
(select sno from Sc inner join C where C.cno = Sc.cno and C.cname='数学');


--2. 查询选修课程编号为 01 的学员姓名和年龄
select S.sname,S.sage
from S inner join Sc on S.sno =Sc.sno
where Sc.cno ='01';


--3. 查询不选修课程编号为 01 的学员姓名和年龄
select S.sname,S.sage
from S inner join Sc on S.sno =Sc.sno
where Sc.cno !='01';


--4. 查询只选修了一门课程的学员姓名和年龄 
select sname,sage from S 
where sno in
(select S.sno
from S inner join Sc on S.sno =Sc.sno
group by Sc.sno having count(*)>1);


--5. 查询选修了课程的学员人数
select count(distinct sno) as '数量' from Sc;


--6. 查询选修课程超过5门的学员学号和姓名
select sno,sname from S 
where sno in
(select S.sno
from S inner join Sc on S.sno =Sc.sno
group by Sc.sno having count(distinct Sc.cno)>5);



--测试三
--1. 找出没有选修过“孟家美”老师讲授课程的所有学生姓名
select sname
from S
where sno not in 
(select Sc.sno
from Sc inner join C on C.cno =Sc.cno
where C.teacher='孟家美'
);

--2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
select S.sname, Avg(Sc.grade)
from S inner join Sc on S.sno=Sc.sno
where Sc.grade<60
group by Sc.sno,S.sname
having count(distinct cno)>=2;


--3. 列出既学过“ 01 ”号课程,又学过“ 02 ”号课程的所有学生姓名
select S.sname
from S
inner join Sc on S.sno=Sc.sno
where  Sc.cno='01' and S.sno in 
(select S.sno
from S
inner join Sc on S.sno=Sc.sno
where  Sc.cno='02');


--5. 列出“ 01 ”号课成绩比“ 02 ”号课成绩高的所有学生的学号及其“ 1 ”号课和“ 2 ”号课的成绩
select Sc1.sno,Sc1.grade as '01号课成绩',Sc2.grade as '02号课成绩'
from Sc Sc1,Sc Sc2
where Sc1.sno =Sc2.sno 
and Sc1.cno='01'
and Sc2.cno='02'
and Sc1.grade >Sc2.grade;









--测试四

--1. 在基本表 SC 中修改 4 号课程的成绩,若成绩小于等于 75 分时提高 5% , 若成绩大于 75 分时提高 4% (用两个 UPDATE 语句实现)。



--2. 把低于总平均成绩的女同学成绩提高 5%



--3. 把选修数据库原理课不及格的成绩全改为空值



--4. 把WANG 同学的学习选课和成绩全部删去。



--5. 在基本表 SC 中删除尚无成绩的选课元组。



--6. 在基本表 S 中检索每一门课程成绩都大于等于 80 分的学生学号、姓名和性别,并把检索到的值送往另一个已存在的基本表 S1 (Sno、SNAME、SSEX )
View Code

2.练习2

--相关表

 --UserInfor表: id name age salary

--相关表

 --Student_Info(学生表):sno、sname、sex、birthDate、homeAddress、remark

 --Curriculum(课程表):cno、cname、cscore(学分)

 --Grade(成绩表):sno、cno、grade(成绩)

练习题分享:(待补充)

--测试1
--相关表
--UserInfor表: id  name  age  salary

--1. 删除姓名、年龄重复的记录,只保留Id最大的一条.
--分析:根据姓名、年龄分组,取出每组的Id最大值,然后将Id最大值之外的排除。

--错误写法:不能先select出同一表中的某些值,再update这个表(在同一语句中),即不能依据某字段值做判断再来更新某字段的值!!!
delete from UserInfor where id not in 
(select Max(id) from UserInfor
group by name,age);

--正确写法:
delete from UserInfor where id not in 
(select * from 
    (select Max(id) from UserInfor group by name,age) a);


--测试2

--相关表
--Student_Info(学生表):sno、sname、sex、birthDate、homeAddress、remark
--Curriculum(课程表):cno、cname、cscore(学分)
--Grade(成绩表):sno、cno、grade(成绩)

--1. 在GRADE表中查找80-90分的学生学号和分数(ok)
select sno,grade from Grade
where grade  between '80' and '90';

--2.在GRADE 表中查找课程编号为003学生的平均分(ok)
select Avg(grade) as '平均分'
from Grade
where cno ='003';

--3.在GRADE 表中查询学习各门课程的人数(ok)
select cno,count(*)
from Grade 
group by cno;

--4.查询所有姓张的学生的学号和姓名(ok)
select sno,sname
from Student_Info
where sname like '张%';

--5.查询和学号’0001’的这位同学性别相同的所有同学的姓名和出生年月(ok)
select sname,birthDate
from Student_Info
where sex =(select sex from Student_Info where sno='0001');

--6.查询所有选修课程编号为0002 和0003的学生的学号、姓名和性别(ok)
select S.sno,S.sname,S.sex
from Student_Info S inner join Grade G on S.sno = G.sno
where G.cno in ('0002','0003');

--7.查询出学号为0001的学生的分数比0002号学生最低分高的课程的课程编号和分数(ok)
select cno,grade
from Grade 
where grade >(select Min(grade) from Grade where sno='0002') and sno='0001';

--8.查询分数在80-90分的学生的学号、姓名、分数(ok)
select S.sno,S.sname,G.grade
from Student_Info S inner join Grade G on S.sno = G.sno
where G.grade between 80 and 90;

--9.查询学习了’C语言’课程的学生学号、姓名和分数(ok)
select S.sno,S.sname,G.grade
from Student_Info S inner join Grade G on S.sno = G.sno
inner join Curriculum C on G.cno=C.cno
where C.cname='C语言';

--10.查询所有学生的总成绩,要求列出学号、姓名、总成绩,没有选课的学生总成绩为空。 (有问题)
select S.sno,S.sname, Sum(G.grade) as '总成绩'
from Student_Info S left join Grade G on S.sno = G.sno
group by G.sno;
View Code

 3. 练习3

 --相关表

 -- Course(课程表):course_id(课程号),name(课程名),teacher_id

 -- Score(成绩表):sid(学号)、course_id(课程号)、score(分数)

 -- Student(学生表):sid、sname、birthDay

 -- Teacher(老师表):teacher_id, name

 -- Dept(部门表):dept_id,name

练习题分享:

--涉及的表
-- Course(课程表):course_id(课程号),name(课程名),teacher_id
-- Score(成绩表):sid(学号)、course_id(课程号)、score(分数)
-- Student(学生表):sid、sname、birthDay
-- Teacher(老师表):teacher_id, name
-- Dept(部门表):dept_id,name

--1. 查看所有英语成绩超过数学成绩的学生的学号和姓名
Select aa.sid,aa.sname
from (Select a1.sid,sname,score from Student a1 inner join Score b1 on a1.sid=b1.sid inner join Course c1 on c1.course_id=b1.course_id where c1.`name`='数学') aa 
inner join (Select a2.sid,sname, score from Student a2 inner join score b2 on a2.sid=b2.sid inner join Course c2 on c2.course_id=b2.course_id where c2.`name`='英语') bb
on aa.sid=bb.sid
Where aa.score < bb.score;

--2. 查看平均成绩大于等于60的所有学生的姓名和平均成绩
select Student.sid,Student.sname,Avg(Score.score)
from Student inner join Score on Student.sid = Score.sid 
GROUP BY Student.sid,Student.sname HAVING Avg(Score.score) >=60;

--3. 查询所有同学的学号、姓名、选课数、总成绩
select S1.sid,S1.sname,count(S2.course_id),sum(S2.score)
from Student S1 inner join Score S2
on S1.sid = S2.sid
group by S1.sid,S1.sname;

--4. 查询姓zhang的老师的个数
select count(*) from Teacher where name like 'zhang%';

--5. 查询没有学过zhangsan老师课程的学生的学号和姓名
select sid ,sname
from Student 
where sid not in 
(select Student.sid
from Student inner join Score on Student.sid=Score.sid
inner join Course on Course.course_id=Score.course_id
inner join Teacher on Course.course_id=Teacher.teacher_id
where Teacher.`name`='zhangsan'
);

--6. 查询既学过英语也学过语文的学生的学号和姓名
Select a.sid,sname from Student a 
inner join Score b on a.sid=b.sid
Inner join Course c on b.course_id=c.course_id
Where c.name in ('英语', '语文')
Group by Sid,sname Having count(*)>=2;

--7. 查询有学生的单科成绩小于60的姓名和课程名称
select Student.sname,Course.`name`
from Student inner join Score on Student.sid = Score.sid
inner join Course on Course.course_id=Score.course_id
where Score.score<60;

--8. 按平均成绩从高到低显示所有学生的姓名和语文、数学、英语三科成绩 
Select c.sname, avg(score) score_avg,
sum(case when b.name='语文'then a.score else 0 end) a1,
sum(case when b.name='数学' then a.score else 0 end) a2,
sum(case when b.name='英语' then a.score else 0 end) a3
From Score a 
inner join Course b on a.course_id=b.course_id
inner join Student c on c.sid=a.sid
Group by a.sid 
order by avg(a.score) desc;

--9. 查询各科成绩中的最高分和最低分
select course_id,max(score),min(score)
from Score
group by course_id;

--10. 计算各科平均成绩和及格率的百分比
 Select course_id,avg(score) as '平均分',
 sum(case when score>=60 then 1 else 0 end)/count(*)*100 as '及格率'
 From Score
 Group by course_id


--11. 查询不同老师所教的不同课程按照平均分从高到低排列(这里假设一个老师就教一门课)
Select c.name '老师名',b.name '课程名',avg(score) '平均分'
From Score a inner join Course b on a.course_id=b.course_id 
Inner join teacher c on b.teacher_id=c.teacher_id 
Group by c.name,b.name 
Order by avg(score) desc

--12. 查询英语和数学课程成绩排名第5到第10的学生的姓名和成绩
(select Student.sname,Score.score
from Score
inner join Student on Score.sid = Student.sid
inner join Course on Course.course_id = Score.course_id
where Course.`name` ='英语'
order by Score.score desc 
limit 4,6)
union all
(select Student.sname,Score.score
from Score
inner join Student on Score.sid = Student.sid
inner join Course on Course.course_id = Score.course_id
where Course.`name` ='数学'
order by Score.score desc 
limit 4,6)

--13. 统计按照各科成绩,分段统计每个课程在90分以上、80-90、60-80、低于60分的人数
Select b.name,
sum(case when score>=90 then 1 else 0 end),
sum(case when score<90 and score>=80 then 1 else 0 end),
sum(case when score<80 and score>=60 then 1 else 0 end),
sum(case when score<60 then 1 else 0 end) 
From Score a
inner join Course b on a.course_id=b.course_id 
Group by b.name;

--14. 查看每门课程被选修的学生数
select course_id,count(*)
from Score
group by course_id;

--15. 查看只学习了一门课程的学生的姓名和学号
select Student.sid,Student.sname
from Student inner join Score on Student.sid =Score.sid
group by Student.sid,Student.sname having count(*)=1;

--16. 查询名字相同的学生名单和个数
select sname,count(*)
from Student
group by sname having count(*)>1;

--17. 查询85年之后出生的学生人数
select count(*)
from Student
where birthDay > '1985-01-01';

--18. 查询每门课程的平均成绩、按升序排序,如果平均成绩相同,按课程ID降序
select course_id,Avg(score)
from Score
group by course_id
order by Avg(score) asc,course_id desc;

--19. 查询有不及格学生的课程和不及格学生个数
Select course_id,count(*) 
From Score 
Where score<60
Group by course_id;

--20. 将所有学生姓名中前后的空格去掉
Update Student set sname=ltrim(rtrim(sname));

--21. 将所有学生的考试成绩展示为课程名:成绩样式
Select a.sid,concat(b.name,':',a.score)
From Score a 
inner join Course b
on a.course_id=b.course_id;

--22. 将所有老师的名字差分成姓 和 名 两个字段显示
Select name, substring(name,1,locate(' ',name)-1),substring(name,locate(' ',name)+1,50) from Teacher;
View Code

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
 
原文地址:https://www.cnblogs.com/yaopengfei/p/14035067.html