Java登陆第三天——SQL之DQL

发布时间 2023-11-10 19:56:48作者: ocraft

SQL语句

SQL概括起来可以分为以下四组。(都是SQL,因为功能的不同,为了更好学习划分了类别)

DDL——数据定义语言。用于定义数据的结构。 指的是增,删,改数据库
DML——数据操作语言。用于检索或修改数据。 指的是增,删,改数据
DQL——数据查询语言。用于查询各种形式的数据。 指的是查询数据
DCL——数据控制语言。用于定义数据库用户的权限。 指的是管理数据库权限

SQL大小写不敏感!!
SQL大小写不敏感!!
SQL大小写不敏感!!

全凭个人习惯

DQL

专门用来查询数据的SQL。

基础查询语句

--查看MySQL版本
select version();

--基础查询,如果字段名填 * 就是所有字段:select * from 表名;
select 字段名 from 表名;

--可以增加查询条件 操作符是WHERE。
select 字段名 from 表名 where 逻辑语句;
--逻辑语句就是:字段名+逻辑运算符和某值,构成一个布尔表达式。

--去重
select distinct 字段名 from 表名;

--从小到大排序 ORDER BY,默认排	序就是从小到大
SELECT 字段名 FROM 表名  ORDER BY 待排序的字段名;

--从大到小排序 在字段名后+DESC
SELECT 字段名 FROM 表名 ORDER BY 待排序的字段名 DESC;

现UserTest库中有一表student,其建表语句和数据如下:

create table student(
id int not null,
name varchar(20) not null default '不知道',
chinese float not null default 0.0 ,
english float not null default 0.0,
math float not null default 0.0
);
insert into student(id,name,chinese,english,math) values(1,'赵信',88.9,72.6,68.3);
insert into student(id,name,chinese,english,math) values(2,'阿卡丽',62.1,49.6,66.6);
insert into student(id,name,chinese,english,math) values(3,'武器',77.1,60.0,32.6);
insert into student(id,chinese,english,math) values(4,99.2,45.1,77.9);
insert into student(id,name,chinese,english,math) values(5,'李刚',50.1,44.4,28.9);

image

栗子:查询name为不知道的所有成绩。
SQL语句:

select * from student where name='不知道';

程序运行结果:
image

栗子:查询所有人的name和chinese成绩
SQL语句:

select name,chinese from student;

程序运行结果:
image

栗子:查询english成绩和math成绩都大于60的人的name。
SQL语句:

select name from student where english>60 and math>60;

程序运行结果:
image

栗子:查询name是两个字的人
SQL语句:

select name from student where name like '__';

程序运行结果:
image

栗子:插入一位同学的成绩,丽桑卓,id为6,chinese77.1,english88.9,math100
然后查询name和chinese,要求从小到大排序

SQL语句:

insert into student(id,name,chinese,english,math) values(6,'丽桑卓',77.1,88.9,100);
select name,chinese from student order by chinese;

程序运行结果:
image

栗子:查询chinese并去重
SQL语句:

select distinct chinese from student;

程序运行结果:
image

注:因为武器和丽桑卓name不同,所以distinct不会去重。
image

表达式查询语句

--在SQL中你甚至可以使用表达式。
select [表达式] from 表名;

--使用 as 关键字。
--允许给字段取别名(不会影响到原表,类似虚拟一张表格。别名为中文时,可以不加引号。)
select 字段名 [as 字段别名] from 表名;

image

栗子:查询name以及chinese+english+math这三科成绩的和。
SQL语句:

select name,(chinese+english+math) from student;

程序运行结果:
image
上述情况是浮点数精度问题

栗子:查询name以及chinese+english+math这三科成绩的和。
和的别名:总分

SQL语句:

select name,(chinese+english+math) as '总分' from student;

程序运行结果:
image

聚合函数

在表达式的基础上,提供了一些系统自带的函数。
聚合函数一般分为两种:数学函数,字符串函数

SELECT 函数(字段) FROM 表名 [where 逻辑语句];

常见的数学函数如下:

数学函数名 说明 (字段也叫列)
COUNT 某一列的数量
SUM 某一列的总和,该列数据类型必须为数值
AVG 某一列的平均值,该列数据类型必须为数值
MAX 某一列的最大值
MIN 某一列的最小值
ABS 绝对值

image
栗子:求math最高分。
SQL语句:

select max(math) from student;

程序运行结果:
image

栗子:求全班人数。
SQL语句:

select count(*) from student;

程序运行结果:
image

如果COUNT函数中填写*不会忽略null。
如果COUNT函数中填写字段名会忽略null

(建表时字段约束都不允许为NULL= =,所以不好展示)

分组查询

分组查询一般是搭配聚合函数一起使用的。

--关键字 GROUP BY 分组字段名,意为按照某字段分组
select 聚合函数(字段名) from 表名 [where 逻辑语句] group by 分组字段名;

--在分组的基础上,可以使用 having 再次过滤
select 聚合函数(字段名) from 表名 [where 逻辑语句] group by 分组字段名 having 逻辑语句;

准备数据

-- 创建订单表
create table t_order(
  id int not null AUTO_INCREMENT COMMENT '订单id',
  user_id bigint not null comment '下单人id',
  user_name varchar(16) not null default '' comment '用户名',
  price decimal(10,2) not null default 0 comment '订单金额',
  the_year SMALLINT not null comment '订单创建年份',
  PRIMARY KEY (id)
) comment '订单表';

-- 插入数据
insert into t_order(user_id,user_name,price,the_year) values
  (1001,'水壶',11.11,'2017'),
  (1002,'萨科',22.22,'2018'),
  (1003,'卡莎',88.88,'2018'),
  (1004,'挖掘机',33.33,'2018'),
  (1005,'扑克牌',12.22,'2018'),
  (1006,'压路机',16.66,'2018'),
  (1007,'牙膏',44.44,'2019'),
  (1008,'中年茶',55.55,'2018'),
  (1009,'纸张',66.66,'2019');

image

栗子:统计不同the_year的数量
SQL语句:

select the_year,count(*) from t_order group by the_year;

程序运行结果:
image

解析:
image

栗子:统计the_year大于2017年的的数量
SQL语句:

select the_year,count(*) from t_order group by the_year having the_year >2017;

程序运行结果:
image

栗子:统计每年最高的price
SQL语句:

select max(price) from t_order group by the_year;

程序运行结果:
image

栗子:统计每年最高的price,并忽略小于等于50
SQL语句:

select max(price) from t_order group by the_year having max(price)>50;

程序运行结果:
image

常见的字符串函数如下:
待编辑