mysql笔记整理3.md

多表查询

primary key; --主键
auto_increment int; --类型自增

笛卡尔积

select * from a,b; --直接查询会造成笛卡尔积
–例如 id name id1 price
– 1 苹果 1 2.3
– 2 橘子 1 2.3
– 3 香蕉 1 2.3
– 1 苹果 2 3.5
– 2 橘子 2 3.5
– 3 香蕉 2 3.5

–为了把冗余数据过滤 可以使用内连接查询
–内连接查询的结果:两表的公共部分。
##内连接查询

–隐式内连接查询:

–语法:select 列名 , 列名 … from 表名1,表名2 where 表名1.列名 = 表名2.列名;

例: select * from a, b where a.id = b.id; --只显示a和b的对应id相同的数据结果

–显式内连接查询:

–语法:select * from 表名1 inner join 表名2 on 条件 ;

例:select * from a inner join b on a.id = b.id;
查询结果:
– id name id1 price
– 1 苹果 1 2.3
– 2 橘子 2 3.5

##外连接查询
外连接:左外连接、右外连接、全外连接。

左外连接:

语法:select * from 表1 left outer join 表2 on 条件;

例:select * from a left outer join b on a.id = b.id;
查询结果:
– id name id1 price
– 1 苹果 1 2.3
– 2 橘子 2 3.5
– 3 香蕉 null null

右外连接:

语法:select * from 表1 right outer join 表2 on 条件;

例:select * from a right join b on a.id = b.id;
查询结果:
– id name id1 price
– 1 苹果 1 2.3
– 2 橘子 2 3.5
– null null 4 null

总结:

– 左外连接以关键字左侧数据为主,不管右侧的数据是否有对应,
– 都把左侧的数据显示出来。
– 右外连接以关键字右侧数据为主,不管左侧的数据是否有对应,
– 都把右侧的数据显示出来。

全外连接:

​ 左外连接和右外连接的结果合并。

语法:select * from 表1 full outer join 表2 on 条件;

– mysql数据库不支持此语法。Oracle和DB2及其他数据库是支持的。
例: select * from a full outer join b on a.id = b.id;
–由于mysql不支持该语法,但是我们可以使用union来达到全外连接的查询效果。
–union :可以将左外连接查询和右外连接查询两条sql语句
– 使用union合并起来进行查询,去掉重复的数据。
例: select * from a left outer join b on a.id = b.id
​ union
​ select * from a right outer join b on a.id = b.id;
查询结果:
– 1 苹果 1 2.3
– 2 橘子 2 3.5
– 3 香蕉 null null
– null null 4 null
如果不想去掉重复的数据可以使用 union all 语句
例: select * from a left outer join b on a.id = b.id
​ union all
​ select * from a right outer join b on a.id = b.id;
查询结果:
– id name id1 price
– 1 苹果 1 2.3
– 2 橘子 2 3.5
– 3 香蕉 null null
– 1 苹果 1 2.3
– 2 橘子 2 3.5
– null null 4 null

多表查询总结:

内连接:
1、 隐式内连接:
Select * from a,b where a.id = b.id;
结果:C
2、 显示内连接:
Select * from a inner join b on a.id = b.id;
结果:C
外连接:
1、 左外连接
select * from a left outer join b on a.id = b.id;
结果:A+C
2、 右外连接
select * from a right outer join b on a.id = b.id
结果:B+C
3、 union:相当于全外连接
select * from a left outer join b on a.id = b.id
union
select * from a right outer join b on a.id = b.id;
结果:A+B+C,会自动滤重

select * from a left outer join b on a.id = b.id
union all
select * from a right outer join b on a.id = b.id;
结果:A+B+C,有重复数据

SQL关联子查询

子查询:把一个sql的查询结果作为另外一个查询的参数存在。
库中有A B两表分别为:
​ A: B:
– id name id name
– 1 苹果 1 2.3
– 2 橘子 2 3.5
– 3 香蕉 4 null
– 需求1:查询价格最贵的水果名称。

  1. 在B表查找最高的价格
    select max(price) from b;
  2. 在B表查找最高价格对应的id
    select b.id from b where b.price = 3.5;
  3. 在A表根据编号找出对应水果名
    select a.name from a where a.id in(2);
    –使用子查询来完成上述的功能需求:
    select a.name from a where a.id in(
    select b.id from b where b.price = (
    select max(price) from b));

in的用法

关联子查询其他的关键字使用:
in 表示条件应该是在多个列值中。
in:使用在where后面,经常表示是一个列表中的数据,
​ 只要被查询的数据在这个列表中存在即可。
– 需求:查询不及格的学生
– 1)查询学生表中不及格的学生id
select student_id from studentcourse where score < 60;
– 2)根据不及格的学生id到学生表中查找学生
select * from student where id in(
select student_id from studentcourse where score < 60);

exists的用法

– 需求:查询不及格的学生
select * from student where exists(
select student_id from studentcourse where score < 60
and studentcourse.student_id = student.id); --不及格的学生id与学生表id对比
– exists的查询方式:将外表中的数据逐行拿到内表中去判断条件是否成立,
– 如果成立返回该行数据。如果不成立,丢弃该行数据。
– (注意:显示的是外表中的数据。)

all的用法

– all:表示所有,和union一起使用。如果在查询时,
– 单独使用union 可以把多个查询的结果进行合并,
– 会过滤掉重复的数据。如果union all 只会简单的把多个查询结果合并。
– 不会过滤掉重复的数据。左连接和右连接查询结果的合集。
举例:
​ a > all(1,2,3) 等价于a > 1,a > 2and a > 3
​ 等价于a > 3(a > 3 必然a > 1和2) 等价于a > max(1,2,3)
​ 同理 a < all(1,2,3) 等价于 a < min(1,2,3)

–需求:查询年龄最大的学生的信息。
– 1) 查询最大的年龄
select max(age) from student;
– 2) 通过年龄查学生信息
–查询(遍历)学生表(集合) 符合最大年龄条件的学生信息
select * from student where age = (select max(age) from student);
使用all代替max查询方法:
select * from student where age >= all(select age from student);
–相当于select * from student where age >= all(学生表里的全部age)

any和some的使用方法,以及as的使用方法.

l any:表示任何一个
举例:
​ a > any(1,2,3) 等价于a > 1或a > 2或a > 3 等价于a > 1
​ 等价于a > min(1,2,3) a > 1包含a > 2和a > 3 范围最广泛
​ 同理 a < any(1,2,3) 等价于 a < max(1,2,3)

–需求:查询成绩是90的学生的信息
– 1) 在中间表中查询成绩为90的学生id
select student_id from studentcourse where score = 90;
– 2) 通过学生id查询学生信息
方法一:使用in --(学生可能为多个in不能使用=)
select * from student where id in(select student_id from studentcourse where score = 90);
方法二:使用any – id=any(1,4)等价于id=1 or id=4,所以结果为2个;
select * from student where id = any(select student_id from studentcourse where score = 90);
方法三:使用some
select * from student where id = some(select student_id from studentcourse where score = 90)
l some: --表示任何一个,和any的作用相同。
any和some 是没有区别的,some和any 效果一样 ,代表一部分记录。

l as:

不仅可以用来做列的别名,还可以将查询结果通过as作为一张表来使用。

–需求: 查询不及格的学生信息和不及格分数
– 1) 在中间表中查询不及格学生id和分数
select student_id,score from studentcourse where score < 60;
–说明:可以把上述查询的结果看作为一张数据库的临时表。
– 2) 在学生表中通过学生的id查询学生信息并显示分数
select student.*,x.score from student,
(select student_id,score from studentcourse where score < 60) as x
where student.id = x.student_id;
– 解析
– 1.(select student_id,score from studentcourse where score < 60) as x
– 把在中间表中查询不及格学生id和分数设为一个临时表x
– 2.对比x表的student_id 和 student表的id
– 3.打印出符合要求的学生的student表内的全部内容

limit的用法

– 作用:限制查询结果返回的数量。

语法: select * from 表名 limit 索引号, 记录数量;

– mysql中limit的用法:返回前几条或者中间某几行数据
举例: select * from 表名 limit 1,4。
– 1表示索引,注意这里的索引从0开始。
– 4表示查询记录数
– 上述就表示从第2条记录开始查询,一共查询4条,即到第5条。(2,3,4,5)
select * from student limit 1,4;

猜你喜欢

转载自blog.csdn.net/qq_39223083/article/details/82842891
今日推荐