mysql ddl dml dcl ddl是什么意思( 四 )


CREATE TABLE t_student (id int(3) primary key auto_increment ,stuname varchar(30) not null ,age int(3) ,sex varchar(3) ,birthday date ,address varchar(50),class_id int(3));CREATE TABLE t_class(class_id varchar(30) PRIMARY KEY ,class_name varchar(30) UNIQUE ,class_desc varchar(50));
单表查询不带条件的查询
# 1.查询出所有的学生信息 所有的学生的所有字段的信息select * from t_student ;# 2.查询出所有的学生的姓名和性别select stuname,sex from t_student;# 3.对查询的表和列设置对应的别名select stuname as '姓名' ,sex as "性别" from t_student; # 别名简写可以省略 as 和 单引号 select stuname 名称 ,sex 性别 from t_student;# 表也可以取别名select t_student.stuname ,t_student.sex from t_student;select t1.stuname,t1.sex from t_student as t1;select t1.stuname,t1.sex from t_student t1;# 自己增加查询的字段select stuname,sex,18 常量 from t_student;# 4.查询出所有的学生信息,并且显示的形式是【张三】18【岁】select stuname,age,concat('【',stuname,'】',age,'【岁】') from t_student;
单表查询带条件的
# 5.查询出学生表中张三的所有的信息select * from t_student where stuname = '张三';# 6.查询出学生表中年龄在18到22之间的学生的所有信息select * from t_student where age >=18 and age <= 22;select * from t_student where age BETWEEN 18 and 22 ;# 7.查询出学生表中编号为1和3的学生信息select * from t_studentwhere id = 1 or id = 3;select * from t_student where id in (1,3)# 8.查询出学生表中地址信息为空的学生信息# 不行 # # select * from t_student where address = '';# select * from t_student where address = null;select * from t_student where address is null;# 不为空的情况select * from t_student where address is not null;# 9.查询出所有姓张的学生的所有信息 -- 模糊查询 likeselect * from t_student where stuname like '张%' ;# 如果不加% 其实和=差不多select * from t_student where stuname like '张三';select * from t_student where stuname like '%三%';# 10.查询出学生表中年龄大于20的男同学的所有信息select * from t_student where age > 20 and sex = '男'# 11 查询出学生表中年龄大于20或者住址在长沙的同学的所有信息select * from t_student where age > 20 or address like '%长沙%'# 12 查询出所有的学生信息,根据id降序 desc 降序 asc 升序【默认就是升序,也就是 asc可以省略】select * from t_student order by id desc;select * from t_student order by id asc;select * from t_student order by id ;# 排序我们可以根据多个字段来排列,前面的字段优先排序# 先根据age降序排列,如果age有相同的信息,那么再根据id升序排序select * from t_student order by age desc ,id asc; select * from t_student order by age desc ,id desc;
4.2 聚合函数聚合函数一般用于统计
# 聚合函数 -- 一般用于统计# 1.统计学员的总数 count 统计某列中非空的数据的条数select count(*) from t_student ;select count(id) from t_student ;select count(address) from t_student;select count(birthday) from t_student;## 在实际开发中我们使用 count(1) 来统计,效率会更高select 1,id from t_student ;select count(1) from t_student ;# 2.统计班级中学生最大的年龄select max(age) from t_student ;# 3.统计班级中学习最小的年龄select min(age) from t_student ;# 4.统计班级中的学员的平均年龄select avg(age) from t_student ;# 5.统计班级中学员的年龄总和select sum(age) from t_student ;
4.3 分组查询语法规则
SELECT <字段列表>FROM <表名>[WHERE <查询条件>][ORDER BY <排序字段>][GROUP BY <分组字段>][HAVING <分组后的查询条件>]
分组查询通常用于统计,一般和聚合函数配合使用
注:分组查询有一个原则,就是 select 后面的所有列中没有使用聚合函数的列,必须出现在group by后面
4.4 常用函数4.4.1 数字函数函数 说明 ABS(x) 返回x的绝对值 AVG(expression) 返回一个表达式的平均值,expression 是一个字段 CEIL(x)/CEILING(x) 返回大于或等于 x 的最小整数 FLOOR(x) 返回小于或等于 x 的最大整数 EXP(x) 返回 e 的 x 次方 GREATEST(expr1, expr2, expr3, …) 返回列表中的最大值 LEAST(expr1, expr2, expr3, …) 返回列表中的最小值 LN 返回数字的自然对数 LOG(x) 返回自然对数(以 e 为底的对数) MAX(expression) 返回字段 expression 中的最大值 MIN(expression) 返回字段 expression 中的最大值 POW(x,y)/POWER(x,y) 返回 x 的 y 次方 RAND() 返回 0 到 1 的随机数 ROUND(x) 返回离 x 最近的整数 SIGN(x) 返回 x 的符号,x 是负数、0、正数分别返回 -1、0 和 1 SQRT(x) 返回x的平方根 SUM(expression) 返回指定字段的总和 TRUNCATE(x,y) 返回数值 x 保留到小数点后 y 位的值(与 ROUND 最大的区别是不会进行四舍五入)

推荐阅读