MySQL基础之数据管理【3】
MySQL中的多表联查
--查询emp的id username age depName
create table emp(
id int unsigned auto_increment key,
username varchar(20) not null unique comment '编号',
age tinyint unsigned not null default 18 comment '年龄',
sex enum('男','女','保密') not null default '保密' comment '性别',
addr varchar(20) not null default '北京',
depId tinyint unsigned not null comment '部门对应的编号'
)engine=innodb charset=utf8;
insert emp(username,age,depId) values('king',24,1),
('queen',25,2),
('imooc',26,1),
('lily',27,1),
('rose',28,3),
('john',29,3);
create table dep(
id tinyint unsigned auto_increment key,
depName varchar(50) not null unique,
depDesc varchar(100) not null
)engine=innodb charset=utf8;
insert dep(depName,depDesc) values('PHP教学部','研发PHP课件'),
('JAVA教学部','研发JAVA课件'),
('WEB教学部','研发WEB课件'),
('IOS教学部','研发IOS课件');
笛卡尔积的形式(不常用)
select emp.id,emp.username,emp.age,dep.depName from emp,dep;
--显示
+----+----------+-----+------------+
| id | username | age | depName |
+----+----------+-----+------------+
| 1 | king | 24 | IOS教学部 |
| 1 | king | 24 | JAVA教学部 |
| 1 | king | 24 | PHP教学部 |
| 1 | king | 24 | WEB教学部 |
| 2 | queen | 25 | IOS教学部 |
| 2 | queen | 25 | JAVA教学部 |
| 2 | queen | 25 | PHP教学部 |
| 2 | queen | 25 | WEB教学部 |
| 3 | imooc | 26 | IOS教学部 |
| 3 | imooc | 26 | JAVA教学部 |
| 3 | imooc | 26 | PHP教学部 |
| 3 | imooc | 26 | WEB教学部 |
| 4 | lily | 27 | IOS教学部 |
| 4 | lily | 27 | JAVA教学部 |
| 4 | lily | 27 | PHP教学部 |
| 4 | lily | 27 | WEB教学部 |
| 5 | rose | 28 | IOS教学部 |
| 5 | rose | 28 | JAVA教学部 |
| 5 | rose | 28 | PHP教学部 |
| 5 | rose | 28 | WEB教学部 |
| 6 | john | 29 | IOS教学部 |
| 6 | john | 29 | JAVA教学部 |
| 6 | john | 29 | PHP教学部 |
| 6 | john | 29 | WEB教学部 |
+----+----------+-----+------------+
内连接的形式(查询两个表中符合连接条件的记录)
--select 字段名称,... from tbl_name1 [inner] join tbl_name2 on 连接条件;
select e.id,e.username,e.age,d.depName
from emp as e
inner join dep as d
on e.depId=d.id;
--显示
+----+----------+-----+------------+
| id | username | age | depName |
+----+----------+-----+------------+
| 1 | king | 24 | PHP教学部 |
| 2 | queen | 25 | JAVA教学部 |
| 3 | imooc | 26 | PHP教学部 |
| 4 | lily | 27 | PHP教学部 |
| 5 | rose | 28 | WEB教学部 |
| 6 | john | 29 | WEB教学部 |
+----+----------+-----+------------+
外连接的形式
--左外连接(select 字段名称,... from tbl_name1 left [outer] join tbl_name2 on 条件;)
--先显示左表中的全部记录,再去右表中查询符合条件的记录,不符合的以null代替
select e.id,e.username,e.age,d.depName,d.depDesc
from emp as e
left outer join dep as d
on e.depId=d.id;
--显示
+----+----------+-----+------------+--------------+
| id | username | age | depName | depDesc |
+----+----------+-----+------------+--------------+
| 1 | king | 24 | PHP教学部 | 研发PHP课件 |
| 2 | queen | 25 | JAVA教学部 | 研发JAVA课件 |
| 3 | imooc | 26 | PHP教学部 | 研发PHP课件 |
| 4 | lily | 27 | PHP教学部 | 研发PHP课件 |
| 5 | rose | 28 | WEB教学部 | 研发WEB课件 |
| 6 | john | 29 | WEB教学部 | 研发WEB课件 |
+----+----------+-----+------------+--------------+
--右外连接(select 字段名称,... from tbl_name1 right [outer] join tbl_name2 on 条件;)
--先显示右表中的全部记录,再去左表中查询符合条件的记录,不符合的以null代替
select e.id,e.username,e.age,d.depName
from emp as e
right outer join dep as d
on e.depId=d.id;
--显示
+------+----------+------+------------+
| id | username | age | depName |
+------+----------+------+------------+
| 1 | king | 24 | PHP教学部 |
| 2 | queen | 25 | JAVA教学部 |
| 3 | imooc | 26 | PHP教学部 |
| 4 | lily | 27 | PHP教学部 |
| 5 | rose | 28 | WEB教学部 |
| 6 | john | 29 | WEB教学部 |
| NULL | NULL | NULL | IOS教学部 |
+------+----------+------+------------+
多表联查的操作
create table user(
id tinyint unsigned auto_increment key comment '编号',
username varchar(20) not null unique comment '用户名',
email varchar(50) not null default '2214@qq.com' comment '邮箱',
proId tinyint unsigned not null comment '用户所在省份的编号'
)engine=innodb charset=utf8;
insert user(username,proId) values('a','1'),
('b','1'),
('c','1'),
('d','2'),
('e','3'),
('f','1'),
('g','1');
create table pro(
id tinyint unsigned auto_increment key comment '编号',
proName varchar(10) not null unique comment '省份名称'
)engine=innodb charset=utf8;
insert pro(proName) values('北京'),('上海'),('深圳');
--查询user: id,username pro: proName
select u.id,u.username,p.proName
from user as u
join pro as p
on u.proId=p.id;
--显示
+----+----------+---------+
| id | username | proName |
+----+----------+---------+
| 1 | a | 北京 |
| 2 | b | 北京 |
| 3 | c | 北京 |
| 4 | d | 上海 |
| 5 | e | 深圳 |
| 6 | f | 北京 |
| 7 | g | 北京 |
+----+----------+---------+
--修改北京为首都
update pro set proName='首都' where id=1;
--显示
+----+----------+---------+
| id | username | proName |
+----+----------+---------+
| 1 | a | 首都 |
| 2 | b | 首都 |
| 3 | c | 首都 |
| 4 | d | 上海 |
| 5 | e | 深圳 |
| 6 | f | 首都 |
| 7 | g | 首都 |
+----+----------+---------+
四个表关联的查询
create table pro2(
id tinyint unsigned not null auto_increment key comment '编号',
proName varchar(10) not null unique comment '省份名称'
)engine=innodb charset=utf8;
insert pro2(proName) values('北京'),('上海'),('深圳');
create table admin(
id tinyint unsigned auto_increment key comment '编号',
username varchar(20) not null unique comment '用户名',
email varchar(50) not null default '24235@qq.com' comment '邮箱',
proId tinyint unsigned not null comment '用户所在省份的编号'
)engine=innodb charset=utf8;
insert admin(username,proId) values('king',1),('queen',2);
create table cate(
id tinyint unsigned auto_increment key comment '编号',
cateName varchar(50) unique comment '商品分类名称',
cateDesc varchar(100) not null default '好东西' comment '商品分类描述'
)engine=innodb charset=utf8;
insert cate(cateName) values('母婴'),('服装'),('电子');
create table product(
id int unsigned auto_increment key comment '编号',
productName varchar(50) not null unique comment '商品名称',
price float(8,2) not null default '12' comment '价格',
cateId tinyint unsigned not null comment '商品所在分类的编号',
adminId tinyint unsigned not null comment '管理员编号'
)engine=innodb charset=utf8;
insert product(productName,price,cateId,adminId) values('iphone9',9888,3,1),
('adidas',388,2,2),
('nike',888,2,2),
('奶瓶',288,1,1);
--查询product:id productName price cate:catename(两个表)
select p.id,p.productName,p.price,c.cateName
from product as p
join cate as c
on p.cateId=c.id;
--查询管理员 id username email pro:proName(两个表)
select a.id,a.username,a.email,p.proName
from admin as a
join pro2 as p
on a.proId=p.id;
--查询product:id productName price
--cate:cateName
--admin:username email
--pro2:proName(四表查询)
select p.id,p.productName,p.price,c.cateName,a.username,a.email,pr.proName
from product as p
join admin as a
on p.adminId=a.id
join cate as c
on p.cateId=c.id
join pro2 as pr
on a.proId=pr.Id
where p.price<1000
order by p.price desc
limit 0,2;
--显示
+----+-------------+--------+----------+----------+--------------+---------+
| id | productName | price | cateName | username | email | proName |
+----+-------------+--------+----------+----------+--------------+---------+
| 3 | nike | 888.00 | 服装 | queen | 24235@qq.com | 上海 |
| 2 | adidas | 388.00 | 服装 | queen | 24235@qq.com | 上海 |
+----+-------------+--------+----------+----------+--------------+---------+
MySQL基础之数据管理【3】的更多相关文章
- MySQL基础之数据管理【4】
外键约束的使用(只有InnoDB存储引擎支持外键) create table news_cate( id tinyint unsigned auto_increment key comment '编号 ...
- MySQL基础之数据管理【5】
子查询的使用 select 字段名称 from tbl_name where col_name=(select col_name from tbl_name); --内层语句查询的结果可以作为外层语句 ...
- MySQL基础之数据管理【2】
where条件筛选记录 select id,username,age from uesr where id=5; alter table user add userDesc varchar(100); ...
- MySQL基础之数据管理【1】
添加记录 insert [into] tbl_name[(col_name,...)] {value|values}(values...); --不指定字段名称时需要按照建表时的字段顺序给每一个字段赋 ...
- MySQL基础----py全栈
目录 MySQL基础----py全栈 一.引言 1.什么是数据? 2.什么是数据库(DB)? 3.什么是数据库管理系统(DBMS)? 4.什么是数据库系统? 5.数据库管理系统由来 6.什么是数据模型 ...
- Mysql基础代码(不断完善中)
Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...
- MYSQL基础操作
MYSQL基础操作 [TOC] 1.基本定义 1.1.关系型数据库系统 关系型数据库系统是建立在关系模型上的数据库系统 什么是关系模型呢? 1.数据结构可以规定,同类数据结构一致,就是一个二维的表格 ...
- 【夯实Mysql基础】记一次mysql语句的优化过程
1. [事件起因] 今天在做项目的时候,发现提供给客户端的接口时间很慢,达到了2秒多,我第一时间,抓了接口,看了运行的sql,发现就是 2个sql慢,分别占了1秒多. 一个sql是 链接了5个表同时使 ...
- MySQL基础(非常全)
MySQL基础 一.MySQL概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access ...
随机推荐
- Python进程池multiprocessing.Pool的用法
一.multiprocessing模块 multiprocessing模块提供了一个Process类来代表一个进程对象,multiprocessing模块像线程一样管理进程,这个是multiproce ...
- vscode自动修复eslint规范的插件及配置
在开发大型项目中,经常都是需要多人合作的.相信大家一定都非常头疼于修改别人的代码的吧,而合理的使用eslint规范可以让我们在代码review时变得轻松,也可以让我们在修改小伙伴们的代码的时候会更加清 ...
- JS基础语法---编程思想和对象
编程思想: 把一些生活中做事的经验融入到程序中 面向过程:凡事都要亲力亲为,每件事的具体过程都要知道,注重的是过程 面向对象:根据需求找对象,所有的事都用对象来做,注重的是结果 面向对象特性: 封装, ...
- c程序内存检测工具 - Valgrind
常用C程序内存泄露检测工具 https://blog.csdn.net/u012662731/article/details/78652651
- 解决element-ui的表格设置固定栏后,边框线消失的bug
如上图所示,边框线消失了,解决方法如下 添加css代码,如果是修改全局,则到全局样式文件添加 .el-table__row{ td:not(.is-hidden):last-child{ right: ...
- WORD 和Uint16的区别
UINT A 16-bit unsigned integer on Windows versions 3.0 and 3.1; a 32-bit unsigned integer on Win32 ...
- Redis安装和基本操作
Redis 简介Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 是属于非关系型数据库1.数据比模型较简单2.需要灵活性更强的IT系统3.对数据库性能 ...
- JUC-0-JUC简介
Java JUC 简介 在 Java 5.0 提供了 java.util.concurrent (简称 JUC )包,在此包中增加了在并发编程中很常用 的实用工具类,用于定义类似于线程的自定义子 ...
- SpringCloud学习笔记(二、SpringCloud Config)
目录: 配置中心简介 SpringCloud Config服务端 SpringCloud Config客户端 动态配置属性bean 一些补充(源码分析):Spring事件监听.健康检查health() ...
- mysql中group by 使用
问题描述 我现在需要查询表test,里面需要安装字段a 进行分组.分组之后还有按照b字段最大的.还要查询出字段c. 我先在使用的数据库是mysql8.0 解决 需注意: group by 分组的时候是 ...