查询的关键字
select
. . .
from
. . .
where
. . .
group by
. . .
having # having必须和group by 联合使用
. . .
order by
. . .
#顺序不可以颠倒
*
*
*distinct #去重 只能出现在字段名的最前面
distinct 字段名
union 连接两个查询结果;需要保证列数相同,列的字段相同
limit 将查询结果的一部分显示出来,通常用在分页查询中
limit startIndex,length 起始下标从0开始
DQL :数据查询语句 (关键字select)
普通查询
- select * from 表名; #查询表中所有数据
- desc 表名; #查询表的结构(全拼 descripe 表名)
- select 字段名 from 表名; #(简单查询)
- select 字段1,字段2 from 表名; #查询多个字段
- select 字段名 as 别名 from 表名; #给查询的列起别名(可省略as)
- select 字段名 ‘别名’ from 表名; #当别名里面有空格或者中文用单引号包裹
条件查询(语法 :select . . . from . . . where 条件;)
条件:
1. = 等于
2.<> 或 != 不等于
3. > 大于
4.< 小于
5.<= 小于等于
6.>= 大于等于
7.between . . . and . . . 两个值之间
8.is null 为null
9.is not null 不为null
10.and 并且
11.or 或者
12.in 包含
13.not 取非
14.like 模糊查询
15.% 匹配任意个字符
16._ 匹配一个字符
排序
1.select 字段 from 表名 order by 排序字段名;#默认升序
2.select 字段 from 表名 order by 排序字段名 desc; #指定降序
3.select 字段 from 表名 order by 排序字段名 asc; #指定升序
4.select 字段 from 表名 order by 排序字段名1 asc,排序字段名2 asc;#多字段
*
连接查询 :
多张表联合起来使用
语法:
select
. . .
from
a
join
b
on
a 和 b 的连接条件
where
条件
根据表连接的方式分类:
*内连接:
1.等值连接
2.非等值连接
3.自连接
*外链接:
1.左外连接
2.右外连接
*全连接:
子查询
select
. . (slect) .
from
. . (slect) .
where
. . (slect) .
DML:数据操作语言,凡是对表中的数据进行增删改的操作 (insect delete updata)
- insect into 表名(字段名1,字段2 . . .) values( 值1 ,值2 , . . .);
- 日期相关:str_to_date('时间字符串' , '时间格式') # varchar 转换为date类型
date_format(日期, '时间格式') # date转换为 varchar类型
时间格式 : %Y 年 %m 月 %d日 %h时 %i分 %s秒
date:包括年月日 datetime:包括年月日时分秒
now() #获取系统当前时间
- updata 表名 set 字段名1 = 值1 ,字段名2 = 值2 , 字段名3 = 值3 . . . where 条件;
- delete from 表名 where 条件;
- truncate table 表名; #删除表中的数据,不支持回滚
DDL:数据定义语言,操作表的结构(create,drop,alter)
- 数据类型:varchar, char, int, bigint, float, double, date, datetime, clob, blob
- create table 表名(字段名1 数据类型,字段名2,数据类型 . . . ) ; #新建表格
字段名 default 数据;设置默认值
- drop table if exists 表名 ; #删除表格(若存在)
- create table 新表名 as select * from 旧表名; #复制表
TCL:是事务控制语言(事物提交:commit ,事物回滚:rollback)
DCL:事物控制语言 (授权:grant 撤销权限: revoke)
约束 :constranit
非空约束:not null
唯一性约束: unique
主键约束: primark key
外键约束: forgien key
检查约束: check
- 导入表:source 路径
- 查看数据库版本号: select version();
- 查看当前使用的数据库:select database();
- 查看有哪些数据库:show databases;
- 查看有哪些表:show tables;
- 终止一条sql语句的输入:\c
单行处理函数(数据处理函数)
特点:一个输入对应一个输出
- lower(字段名) ; 转换小写
select lower(字段名) from 表名;
- upper(字段名) ; 转换大写
select upper(字段名) from 表名;
- substr(字段名,起始位置,截取长度) ; 截取字符串(从1开始)
select substr(字段名,起始位置,截取长度) from 表名;
- length(字段名) 长度
select length(字段名) from 表名;
- trim(str) 去空格
select 字段名 from 表名 where 字段名 = trim(‘ str ’);
- str_to_date() 字符串转换为日期
- format() 数字格式化
- round(数据,保留的小数位数) 四舍五入
select roung(数据,保留小数位数) from 表名;
- rand() 取随机数
select round(rand()*10,0) from 表名; #生成0,10的随机数
- ifnull() 可以将null转换为一个具体值
处理null,为了数学运算
select ifnull(字段名,用来替换null的值) from 表名
- concat(str1,str2) 字符串拼接
- case . . . when . . .then . . .when . . .then . . .else . . .end
select ename,job,(case job when 'clerk' then sal*1.1 when 'salesman' then sal + 100 else sal end) as newsal from emp;
分组函数(多行处理函数)
特点:一个输入对应一个输出(必须先分组才可以使用)(分组函数自动忽略null)
- count(字段名) #计数 (不为null的)
select count(字段名) from 表名;
- sum(字段名) #求和
select sum(字段名) from 表名;
- avg (字段名) #平均值
select avg(字段名) from 表名;
- max (字段名) #最大值
select max(字段名) from 表名;
- min(字段名) #最小值
select min(字段名) from 表名;
分组查询
select . . . from . . . group by . . .