04 mysql 基础三 (进阶)

mysql 基础三

阶段一 mysql 单表查询

1.查询所有记录
select *    from department;
?
select *   from student;
?
select *   from student_detail;
2.查询选中列记录
select s_name from student;
3.查询指定条件下的记录
select s_name from student where s_id>2;
4.查询后为列取别名
select s_name as 姓名 from student;
5.模糊查询
select * from student where s_name like ‘赵%‘    # %代表多个字符
select * from student where s_name like ‘_春_‘   # _代表一个字符
6.排序 order by : asc升序(默认) desc降序
select * from student order by dept_id;     # 升序
?
select * from student order by dept_id desc;    # 降序
7.限制显示数据的数量LIMIT
# 按学号升序输出前2条数据
select * from student order by s_id limit 2;
# 按学号升序输出 第3条数据后面的2条数据
select * from student order by s_id limit 3,2;
8.常用聚合函数
#求最大年龄
select max(age) from stu_detail;
#求最小年龄
select min(age) from stu_detail;
#求和
select sum(age) from stu_detail;
#求平均数
select avg(age) from stu_detail;
#四舍五入
select round(avg(age)) from stu_detail;
#统计
select count(age) from stu_detail;
9.分组查询 group by
# 对学生表中学院栏进行分组,并统计每个学院各有多少学生
select dept_id 学院,count(dept_id) 学生个数 from student group by dept_id;
?
# having 分组条件
# having 后的字段必须是select 后出现过的
?
# 查看哪些学院,只有一个学生
select dept_id 学院,count(dept_id) 学生个数 from student group by dept_id
    -> having count(dept_id)=1;

阶段二 musql 子查询

出现在其他SQL语句内的SELECT字句。(select 中 嵌套 select )

# 查出潭州学院中 ‘软件学院‘和‘外语学院‘ 的 id
select tz_id from tanzhou where tz_name=‘软件学院‘ or tz_name=‘外语学院‘;
+-------+
| tz_id |
+-------+
|     1 |
|     3 |
+-------+
 
# 查出学生表中属于‘软件学院‘ 和 ‘外语学院‘ 的学员
select * from student where dept_id in( select tz_id from tanzhou where tz_name=‘软件学院‘ or tz_name=‘外语学院‘);
+------+--------------+---------+
| s_id | s_name       | dept_id |
+------+--------------+---------+
|    1 | 张三         |       3 |
|    3 | 王六         |       1 |
|    6 | 隔壁老王     |       3 |
+------+--------------+---------+

阶段三 mysql 关联查询

1. 内连接 [inner | cross] join
  • 无条件内连接: 又名交叉连接/笛卡尔连接

第一张表种的每一项会和另一张表的每一项依次组合

select * from student inner join tanzhou;
  • 有条件内连接:

    在无条件的内连接基础上,加上一个ON子句

    当连接的时候,筛选出那些有实际意义的记录行来进行拼接

select * from student inner join tanzhou on dept_id=tz_id;
2. 外连接 { lifet | right} join
  • 左外连接: (以左表为基准)

两张表做连接的时候,在连接条件不匹配的时候

留下左表中的数据,而右表中的数据以NULL填充

select * from tanzhou left join student on tz_id=dept_id;
?
+-------+--------------+------+--------------+---------+
| tz_id | tz_name      | s_id | s_name       | dept_id |
+-------+--------------+------+--------------+---------+
|     3 | 外语学院     |    1 | 张三         |       3 |
|     2 | 艺术学院     |    2 | 李四         |       2 |
|     1 | 软件学院     |    3 | 王六         |       1 |
|     4 | 语言学院     |    4 | 陈七         |       4 |
|     2 | 艺术学院     |    5 | 郭伟涛       |       2 |
|     3 | 外语学院     |    6 | 隔壁老王     |       3 |
|     5 | 电竞学院     | NULL | NULL         |    NULL |
+-------+--------------+------+--------------+---------+
  • 右外连接 right join

右外连接: (以右表为基准)

对两张表做连接的时候,在连接条件不匹配的时候

留下右表中的数据,而左表中的数据以NULL填充

select * from student right join tanzhou on tz_id=dept_id;
?
+------+--------------+---------+-------+--------------+
| s_id | s_name       | dept_id | tz_id | tz_name      |
+------+--------------+---------+-------+--------------+
|    1 | 张三         |       3 |     3 | 外语学院     |
|    2 | 李四         |       2 |     2 | 艺术学院     |
|    3 | 王六         |       1 |     1 | 软件学院     |
|    4 | 陈七         |       4 |     4 | 语言学院     |
|    5 | 郭伟涛       |       2 |     2 | 艺术学院     |
|    6 | 隔壁老王     |       3 |     3 | 外语学院     |
| NULL | NULL         |    NULL |     5 | 电竞学院     |
+------+--------------+---------+-------+--------------+
  • 查询没有学员的学院(电竞学员)
select * from student right join tanzhou on tz_id=dept_id where s_id is null;
?
+------+--------+---------+-------+--------------+
| s_id | s_name | dept_id | tz_id | tz_name      |
+------+--------+---------+-------+--------------+
| NULL | NULL   |    NULL |     5 | 电竞学院     |
+------+--------+---------+-------+--------------+
  • 外连接多张表

    mysql> select s_id,s_name,dept_id,tz_name,deptc_id,c_name from student left join tanzhou on dept_id=tz_id left join course on tz_id=deptc_id;
    
    +------+--------------+---------+--------------+----------+--------+
    | s_id | s_name       | dept_id | tz_name      | deptc_id | c_name |
    +------+--------------+---------+--------------+----------+--------+
    |    3 | 王六         |       1 | 软件学院     |        1 | python |
    |    2 | 李四         |       2 | 艺术学院     |        2 | java   |
    |    5 | 郭伟涛       |       2 | 艺术学院     |        2 | java   |
    |    1 | 张三         |       3 | 外语学院     |        3 | c/c++  |
    |    6 | 隔壁老王     |       3 | 外语学院     |        3 | c/c++  |
    |    4 | 陈七         |       4 | 语言学院     |        4 | 外语   |
    +------+--------------+---------+--------------+----------+--------+

原文地址:https://www.cnblogs.com/zcmq/p/9165230.html

时间: 2024-07-31 05:46:48

04 mysql 基础三 (进阶)的相关文章

01 mysql基础一 (进阶)

mysql基础一 1.认识mysql与创建用户 01 Mysql简介 Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQLAB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性. (开源,免费) #关系型数据库:采用关系模型来组织数据的数据库 #关系:一张二维表,每个关系都有一个关系名,就是表名,互相关联 #模型:行和列(二维),具体指字段跟字段信息 02 进入my

mysql基础三(视图、触发器、函数、存储过程、事务、防注入)

一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 1.创建视图 -格式:CREATE VIEW 视图名称 AS SQL语句 CREATE VIEW v1 AS SELET nid, name FROM A WHERE nid > 4 2.删除视图 --格式:DROP VIEW 视图名称 DROP VIEW v1 3.修改视图 -- 格式:ALTER VIEW 视图名称 AS SQL

MySQL基础 三 数值类型,索引

My 1am,1pm 数值类型 Int(4) Float(5,2) Varchar(4) 枚举类型 Enum('female','male') Set('book','football','A'-.最多64个) 约束条件 Null                         Default                        Extra 默认为允许                   默认null Not null                     default 值 Key

MySQL基础三

1 SQL99 1.1 SQL99语法 select 查询列表 from 表1 [连接类型] join 表2 on 连接条件 where 筛选条件; 连接类型: 内连接  :inner 外连接: 左外连接: left outer 右外连接:right outer 交叉连接:cross 1.2 SQL99语法的内连接 1.2.1 内连接的语法 select 查询列表 from 表1 inner join 表2 on 连接条件 [where 筛选条件]; 1.2.2 应用 示例:查询员工名和部门名

Mysql基础(三)

#DML语言 /* 数据操作语言 插入:insert insert into 表名(列名,...) values(值1,...); insert into 表名 set 列名=值, 列名=值,... 修改:update 删除:delete */ CREATE TABLE beauty( id INT(11) PRIMARY KEY, `name` VARCHAR(50), sex CHAR(1), borndate DATETIME, phone VARCHAR(11), photo BLOB,

MySQL基础(三):过滤数据

过滤数据 使用SELECT语句的WHERE子句指定搜索条件. 使用WHERE子句 在SELECT语句中,数据根据WHERE子句中指定的搜索条件进行过滤. WHERE子句在表名(FROM子句)之后给出,如下所示: 输入 SELECT prod_name, prod_price FROM products WHERE prod_price = 2.50; 输出 分析 这条语句从products表中检索两个列,但不返回所有行,只返回prod_price值为2.50的行. 注意:在同时使用ORDERBY

MySql基础教程(三)——查询训练

在MySql两轮基础的学习之后,来一波实战演习... 三张表:学生表,课程表,成绩表. 建表详细信息见 MySql基础教程(一)

MySQL基础(三)——约束

MySQL基础(三)--约束 1.约束的使用方法 2.指定约束的时间 3.not null约束 4.unique约束 5.primary key约束 6.foreign key约束 7.check约束

MYSQL基础笔记(三)-表操作基础

数据表的操作 表与字段是密不可分的. 新增数据表 1 Create table [if not exists] 表名( 2 字段名 数据类型, 3 字段名 数据类型, 4 字段n 数据类型 --最后一行不需要加逗号 5 )[表选项]; If not exists:如果表名不存在,那么就创建,否则不执行创建代码,实现检查功能. 表选项:控制表的表现 1.字符集:charset/character 具体字符集:--保证表中数据存储的字符集. 2.校对集:collate 具体校对集: 3.存储引擎:e