mysql 子查询种类及联合查询的sql语句写法

子查询:

将一个查询语句嵌套在另一个查询语句中,内层查询语句的查询结果可以作为外层查询语句提供条件。

1.in ,not in

2.比较运算符>=

select id,name from student where score >=(select level from scholarship where id=1)

3.[not] exists

select  id,username from employee where exists(select id   from department where id=5)

4.any|some  all配合>=比较运算符

select id,name from student where score >=any(select level from scholarship)

5.将查询结果写入数据表   保证字段顺序,字段数目一致test1的列得存在

insert test1(id,num) select id,score from student;

6.创建数据表同时将查询结果写入到数据表   如果字段名称不一致,以查询出来的为主,原字段为null

create table [if not exists] employee(id,score) select id,score from student;

联合查询:

union去掉重复的 union all简单并集   两张表的查询的列数量一样,类型可以不一样

select id from bbs_addr union select name from bbs_brand;  id是BigInt  name是varchar类型

猜你喜欢

转载自blog.csdn.net/qq_34412985/article/details/85850300