标签:年龄 一个 name rem primary 比较运算符 查询 values 技术
连#建表
create table a(
id int,
name varchar(20)
);
create table b(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技术 |
| 201 | 人力资源 |
| 202 | 销售 |
| 203 | 运营 |
+------+--------------+
#插入数据
insert into a values
(200,'技术'),
(201,'人力资源'),
(202,'销售'),
(203,'运营');
insert into b(name,sex,age,dep_id) values
('egon','male',18,200),
('alex','female',48,201),
('wupeiqi','male',38,201),
('yuanhao','female',28,202),
('liwenzhou','male',18,200),
('jingliyang','female',18,204)
;
+----+------------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+------------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+------+--------+
# 内连接 inner join ... on 连接条件
select * from 表1 inner join 表2 on 条件
select * from a inner join b on a.id=b.dep_id;d
a --> id : 200,201,202,203
b --> dep_id: 200,201,202,204
# 内连接 :只显示两张表中互相匹配的项,其他不匹配的不显示
+------+--------------+----+-----------+--------+------+--------+
| id | name | id | name | sex | age | dep_id |
+------+--------------+----+-----------+--------+------+--------+
| 200 | 技术 | 1 | egon | male | 18 | 200 |
| 201 | 人力资源 | 2 | alex | female | 48 | 201 |
| 201 | 人力资源 | 3 | wupeiqi | male | 38 | 201 |
| 202 | 销售 | 4 | yuanhao | female | 28 | 202 |
| 200 | 技术 | 5 | liwenzhou | male | 18 | 200 |
+------+--------------+----+-----------+--------+------+--------+
结构:select * from 表1 inner join 表2 on 条件
只显示两张表中互相匹配的项,其他不匹配的不显示
连表查询
利用 笛卡尔积 拼接
select * from a,b ;
先计算两张表的笛卡尔积,再根据用户给出的条件进行筛选
select * from a,b where a.id = b.dep_id;
# 内连接 inner join ... on 连接条件
select * from 表1 inner join 表2 on 条件
select * from a inner join b on a.id=b.dep_id;d
a --> id : 200,201,202,203
b --> dep_id: 200,201,202,204
# 内连接 :只显示两张表中互相匹配的项,其他不匹配的不显示
+------+--------------+----+-----------+--------+------+--------+
| id | name | id | name | sex | age | dep_id |
+------+--------------+----+-----------+--------+------+--------+
| 200 | 技术 | 1 | egon | male | 18 | 200 |
| 201 | 人力资源 | 2 | alex | female | 48 | 201 |
| 201 | 人力资源 | 3 | wupeiqi | male | 38 | 201 |
| 202 | 销售 | 4 | yuanhao | female | 28 | 202 |
| 200 | 技术 | 5 | liwenzhou | male | 18 | 200 |
+------+--------------+----+-----------+--------+------+--------+
?```
结构:select * from 表1 left join 表2 on 条件
不管左表中是不是匹配上都会显示所有内容
结构:select * from 表1 right join 表2 on 条件
不管右表中是不是匹配上都会显示所有内容
select * from 表1 left join 表2 on 条件 union select * from 表1 right join 表2 on 条件
子查询是将一个查询语句嵌套在另一个查询语句中。
内层查询语句的查询结果,可以为外层查询语句提供查询条件。
子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
还可以包含比较运算符:= 、 !=、> 、< 等
# 查询平均年龄在25岁以上的部门名
select id,name from department
where id in
(select dep_id from employee group by dep_id having avg(age) > 25);
# 查看技术部员工姓名
select name from employee
where dep_id in
(select id from department where name='技术');
select name from emp;
select name as n,(select age from employee where name = n) from emp;
标签:年龄 一个 name rem primary 比较运算符 查询 values 技术
原文地址:https://www.cnblogs.com/saoqiang/p/11377535.html