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 | 外语 |
    +------+--------------+---------+--------------+----------+--------+

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

  1. 01 mysql 基础一 (进阶)

    mysql基础一 1.认识mysql与创建用户 01 Mysql简介 Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQLAB公司开发,目前属于Oracle公司. MySQL是一种关联数据库 ...

  2. 02 mysql 基础二 (进阶)

    mysql 基础二 阶段一 表约束 1.not null 非空约束 例子: create table tb1( id int, name varchar(20) not null ); 注意 空字符不 ...

  3. mysql基础sql进阶

    回顾前面的基础命令语句 修改数据表 添加字段: alter table 表名 add 字段名 列类型[not null|null][primary key][unique][auto_incremen ...

  4. mysql基础(三)——中级查询

    创建表 CREATE TABLE DEPT( DEPTNO ) PRIMARY KEY, DNAME ) , LOC ) ) ; ,'ACCOUNTING','NEW YORK'); ,'RESEAR ...

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

    一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 1.创建视图 -格式:CREATE ...

  6. MySQL 基础三 函数(聚合、字符串、时间、条件判断)

    1.聚合 其它:GROUP_CONCAT.avg.sum.count.max.min SELECT typeid,GROUP_CONCAT(goodsname) FROM `goods` GROUP ...

  7. Mysql基础(三)

    #DML语言 /* 数据操作语言 插入:insert insert into 表名(列名,...) values(值1,...); insert into 表名 set 列名=值, 列名=值,... ...

  8. python进阶08 MySQL基础补充

    python进阶08 MySQL基础补充 本次课程都是基于三张表格的使用 一.子查询 #如何找到‘张三’的成绩 #思路:先找到张三的学号,在拿这个张三的学号到成绩表里面去匹配,得出成绩 #如何用一条查 ...

  9. MySQL基础(三)——约束

    MySQL基础(三)--约束 约束是在表上强制执行的数据校验规则,主要用于维护表中数据的完整性以及当数据之间有以来关系时,保护相关的数据不会被删除. 根据约束对列的限制,可以划分为:单列约束(只约束一 ...

随机推荐

  1. ExecutorService的invokeAny方法

    一.此方法获得最先完成任务的结果,即Callable<T>接口中的call的返回值,在获得结果时,会中断其他正在执行的任务 示例代码: import java.util.ArrayList ...

  2. js 时间特效

    http://example.com:1234/test.htm#part2:Hash的作用. http://www.cnblogs.com/Interkey/p/RunAsAdmin.html

  3. 再学UML-UML用例建模解析(一)

    UML(统一建模语言)是当前软件开发中使用最为广泛的建模技术之一,通过使用UML可以构造软件系统的需求模型(用例模型).静态模型.动态模型和架构模型.UML通过图形和文字符号来描述一个系统,它是绘制软 ...

  4. 增加C盘空间大小

    随着我们使用电脑的时间越来越久,电脑C盘的空间会出现不够用的情况,这时我们需要的就是增加C盘的大小,基本上有两种方式 1.通过系统自带的磁盘管理(有可能没法操作,主要介绍第二种) 2.通过分区软件进行 ...

  5. spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...

  6. day3-购物车小程序

    1.要求 启动程序后,让用户输入工资,然后打印商品列表 允许用户genuine商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒还有多少 可随时退出,退出时,打印已购买商品和余 ...

  7. 每日linux命令之kill

    1.命令格式: kill[参数][进程号] 2.命令功能: 发送指定的信号到相应进程.不指定型号将发送SIGTERM(15)终止指定进程.如果任无法终止该程序可用“-KILL” 参数,其发送的信号为S ...

  8. LA 3126 出租车

    题目链接:https://vjudge.net/problem/UVALive-3126 题意:有m个客人,位于不同的位置,去一些地方,出发的时间给出,要一些出租车去接,但是,每辆出租车要在出发前一分 ...

  9. AIM Tech Round 5 (rated, Div. 1 + Div. 2) C. Rectangles 【矩阵交集】

    题目传传传送门:http://codeforces.com/contest/1028/problem/C C. Rectangles time limit per test 2 seconds mem ...

  10. 从命令行运行Jmeter及jmeter参数说明、Html报告生成

    为什么要命令行执行脚本,主要有以下三点: 1) 图形化界面消耗更多资源,CPU和内存 2) 图形化界面不支持大型的负载测试和性能测试 3) 命令行测试支持持续集成,例如放到Jenkins这样的CI工具 ...