2022-8-16---MySQL数据库(2)---DQL数据库查询语言

本文详细介绍了MySQL数据库的DQL查询语言,包括单表查询、分组查询、排序、聚合函数、分页查询及多表查询。内容涵盖从基本概念到高级用法,如子查询、连接查询及其不同类型的内外连接。通过实例解析,帮助读者掌握数据库查询技巧。
摘要由CSDN通过智能技术生成

目录

一、构建数据库

1、概念

2、新建四张表

(1)student表

(2)course表

(3)teacher表

(4)scores表

(5)给四张表插入数据

二、单表查询

1、查询所有记录

2、查询指定的列

3、去重

4、列运算

5、别名

6、条件控制

(1)条件查询

 (2)模糊查询

7、排序

(1)升序---ASC(ASC是可以省略)

(2)降序---DESC

(3)使用多列作为排序条件

8、聚合函数

(1)count

(2)max

(3)min

(4)sum

(5)avg

5、分组查询(有难度)

(1)概念

(2)分组查询前

(3)分组查询后

(4)面试题: where和having的区别?

6、分页查询

(1)limit字句

(2)面试题:不同数据库的特有查询语法

(3)分页查询案例分析

7、案例

三、多表查询

1、笛卡尔乘积

2、多表连接的方式

2、SQL92语法

(1)二表查询

(2)三表查询

(3)四表查询

(4)综合查询

(5)案例示例

3、SQL99语法

(1)内连接

(2)外连接(常用)

         ①左连接

         ②右连接

(3)内连接和外连接的区别

(4)全连接

4、子查询

(1)分类

(2)标子量查询

(3)列子查询​​​​​​​

(4)行子查询

(5)表子查询

(6)总结​​​​​​​

一、构建数据库

1、概念

重点:DQL是我们每天都要接触编写最多也是最难的SQL,该语言用来查询记录,它不会修改数据库和表结构。

2、新建四张表

(1)student表

DROP TABLE IF EXISTS student;/*如果存在student表就删除*/
CREATE TABLE `student` (
	`id` INT ( 10 ) PRIMARY KEY,/*主键*/
	`name` VARCHAR ( 10 ),
	`age` INT ( 10 ) NOT NULL,
    `gender`VARCHAR(2)
);

(2)course表

DROP TABLE IF EXISTS course;
CREATE TABLE `course` (
	`id` INT ( 10 ) PRIMARY KEY,/*主键*/
	`name` VARCHAR ( 10 ),
	`t_id` INT(10)
);

(3)teacher表

DROP TABLE IF EXISTS teacher;
CREATE TABLE `teacher` (
	`id` INT ( 10 ) PRIMARY KEY,/*主键*/
	`name` VARCHAR ( 10 )
);

(4)scores表

DROP TABLE IF EXISTS scores;
CREATE TABLE `scores` (
	`s_id` INT ( 10 ),
	`score` INT ( 10 ),
	`c_id`int(10),
	PRIMARY KEY(s_id,c_id)
);

(5)给四张表插入数据

insert into  student (id,name,age,gender)VALUES(1,'张小明',19,'男'),(2,'李小红',19,'男'),(3,'小刚',24,'男'),(4,'小龙',11,'男'),(5,'小丽',18,'男'),(6,'张小军',18,'女'),(7,'小航',16,'男'),(8,'小亮',23,'男'),(9,'小杰',22,'女'),(10,'李小虎',21,'男');

insert into  course (id,name,t_id)VALUES(1,'数学',1),(2,'语文',2),(3,'c++',3),(4,'java',4),(5,'php',null);


insert into  teacher (id,name)VALUES(1,'Tom'),(2,'Jerry'),(3,'Tony'),(4,'Jack'),(5,'Rose');


insert into  scores (s_id,score,c_id)VALUES(1,80,1);
insert into  scores (s_id,score,c_id)VALUES(1,56,2);
insert into  scores (s_id,score,c_id)VALUES(1,95,3);
insert into  scores (s_id,score,c_id)VALUES(1,30,4);
insert into  scores (s_id,score,c_id)VALUES(1,76,5);

insert into  scores (s_id,score,c_id)VALUES(2,35,1);
insert into  scores (s_id,score,c_id)VALUES(2,86,2);
insert into  scores (s_id,score,c_id)VALUES(2,45,3);
insert into  scores (s_id,score,c_id)VALUES(2,94,4);
insert into  scores (s_id,score,c_id)VALUES(2,79,5);

insert into  scores (s_id,score,c_id)VALUES(3,65,2);
insert into  scores (s_id,score,c_id)VALUES(3,85,3);
insert into  scores (s_id,score,c_id)VALUES(3,37,4);
insert into  scores (s_id,score,c_id)VALUES(3,79,5);

insert into  scores (s_id,score,c_id)VALUES(4,66,1);
insert into  scores (s_id,score,c_id)VALUES(4,39,2);
insert into  scores (s_id,score,c_id)VALUES(4,85,3);

insert into  scores (s_id,score,c_id)VALUES(5,66,2);
insert into  scores (s_id,score,c_id)VALUES(5,89,3);
insert into  scores (s_id,score,c_id)VALUES(5,74,4);


insert into  scores (s_id,score,c_id)VALUES(6,80,1);
insert into  scores (s_id,score,c_id)VALUES(6,56,2);
insert into  scores (s_id,score,c_id)VALUES(6,95,3);
insert into  scores (s_id,score,c_id)VALUES(6,30,4);
insert into  scores (s_id,score,c_id)VALUES(6,76,5);

insert into  scores (s_id,score,c_id)VALUES(7,35,1);
insert into  scores (s_id,score,c_id)VALUES(7,86,2);
insert into  scores (s_id,score,c_id)VALUES(7,45,3);
insert into  scores (s_id,score,c_id)VALUES(7,94,4);
insert into  scores (s_id,score,c_id)VALUES(7,79,5);

insert into  scores (s_id,score,c_id)VALUES(8,65,2);
insert into  scores (s_id,score,c_id)VALUES(8,85,3);
insert into  scores (s_id,score,c_id)VALUES(8,37,4);
insert into  scores (s_id,score,c_id)VALUES(8,79,5);

insert into  scores (s_id,score,c_id)VALUES(9,66,1);
insert into  scores (s_id,score,c_id)VALUES(9,39,2);
insert into  scores (s_id,score,c_id)VALUES(9,85,3);
insert into  scores (s_id,score,c_id)VALUES(9,79,5);

insert into  scores (s_id,score,c_id)VALUES(10,66,2);
insert into  scores (s_id,score,c_id)VALUES(10,89,3);
insert into  scores (s_id,score,c_id)VALUES(10,74,4);
insert into  scores (s_id,score,c_id)VALUES(10,79,5);

二、单表查询

1、查询所有记录

基本语法(注意:在开发中,严禁使用select * from)

select * from 表名;
select * from student;

2、查询指定的列

select `id`, `name`,`age`, `gender` from student;
select `id`, `name`, `age` from student;

3、去重

如果表中有完全重复的记录只显示一次,在查询的列之前加上distinct。

select DISTINCT `字段名` from `表名`;

4、列运算

select `id`,`name`,age/10 from student;

注意:

①我们写的所有的查询语句,最终执行的结果,都是生成一张虚拟表。
②null值和任何值做计算都为null ,null不意味着0。

比如在员工表中,给工资都加1000,有工资的加减,有的员工工资初始值,设置了空值。

select id, `name`,sa1+1000 from emp emloyee;

null值和任何值做计算都为null ,需要用到函数ifnull()函数。如果薪资是空,则为0。将字符串做加减乘除运算,会把字符串当0处理。

select IFNULL(sal,0) + 1000 from employee;

5、别名

我们可以给列起别名 因为我们在查询过程中,列名很可能重复,可能名字不够简洁,或者列的名字不能满足我们的要求。

select `id`as `编号`, `name` as`姓名^ ,`age`as `年龄` ,`gender`as `性别` from student ;

除了使用关键字 as 来给表或是 列起别名外,还可以直接使用空格字符达到同样的效果

<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值