DDL , DML , DQL
DQL: Database Query Language
select 结构:
select distance[字段列表 | 聚合函数, 子查询]
from [表名 | 子查询]
where 条件
group by 分组字段名
having 条件
order by [asc | desc]
limit 0,5;
1. 有条件查询
select * from 表名 where [] group by 分组字段名 having []
>,< , >= ,<= , != , <> , =
select * from user where sal <> 800
between a and b; >= , <=
and 并且 , or 或者 ,is not null , is null
in(2,3) , not in(3,4);
别名: as
select count(1) as 总记录数 from user; 查询user表中 的总记录数
distinct 去重 修饰字段 放在select 后面
having: 过滤 , 用法是放在group by 后面 ,
先分组,将分组的结果集 在进行过滤
模糊查询:
select * from `user` where name like '张%';
通配符 % , _ 占位符 1
排序: order by age,name asc | desc
创建订单表
create table `order`(
)
分页: limit 开始行,页大小
startRow , PageSize
limit 5; 第一页
limit 5,5;
int startRow = (pageNo-1)*pageSize;
subList 假分页
limit 0,5 物理分页
按钮式 , 加载式
分组: group by
分组函数, 聚合函数:
select 分组字段,count(1) group by 分组的字段
count(1), avg(); sum(), max(), min()
avg() , sum(); 括号中要放数字
联合查询:
union [distinct]去重, union all 不去重
前提 字段个数相同, 顺序, 类型相同
要把多个表和并成一个结果集的时候
子查询:
什么时候会出现子查询?
查询 办公地点为 NEW YORK ,的人的姓名
SELECT * from emp where deptno = (
select deptno from dept where loc = 'NEW YORK');
select [子查询] from [子查询];
where [子查询] group by 字段 having [子查询]
EXISTS(子查询); 一定是跟子查询连用 ,返回 true , false;
any 跟子查询连用
必须 和 比较运算符连用 > ,< , >= ,<= , =连用
在子查询的结果中匹配就满足条件 ,即 输出
some: some 是 any 别名
2. 无条件查询
select * from 表名 order by 字段名 [asc | desc] limit 0,5}
-----------------------------------------------------------------------
表关联查询 :
级联查询: 多表查询
例如: 我想查看 张三的 所有订单
导入sql 脚本的方式:
1.doc窗口导入 source E:\Users\Administrator\Desktop\girls.sql
2.Navicat 导入
多表查询类型划分:
sql92版本;
select * from beauty,boys
where beauty.boyFriend_id = boys.id;
1.内连接
|-- 等值连接:
select * from beauty,boys
where beauty.boyFriend_id = boys.id;
|-- 不等值连接
|-- 自连接
2.sql92版本 对 外连接支持的不好, 所以现在都用 sql99
sql99版本
1.内连接
|-- 等值连接 inner join on =
|-- 不等值连接 inner join on 不等的条件
|-- 自连接 inner join on 自己连自己
2.外连接:
|-- 左外连接 left [outer] join on
select * from beauty b
left join boys s
on b.boyfriend_id = s.id;
左外连接 是left join左左边的表为主表,右边 的表为从表
从表没有的 使用 null 补位;
|-- 右外连接 right [outer] join on
select * from boys s
right join beauty
|-- 全外连接 full [outer] join mysql 不支持
交叉连接:cross join
就是为了让当前结果集 出现 笛卡尔积现象
truncate 单独使用就是删除表中的数据,不能回滚
delete from 删除表数据, 但是能回滚
=-====================================================================
函数:
数学函数:
ABS(-9); 绝对值
bin(4); 二进制 , OCT()八进制 , HEX() 16进制
greatest(4,5,4,3,9) 找集合中的最大值
least(7,53,5,1,12); 找集合中的最小值
mod(a,b) 取模 , select 7%3 , select 7 mod 3;
select mod(7,3);
PI();
rand(); 返回0-1之间的随机数,但是是 小数
round(x,y) x 是小数, y 小数点 后面的位数 , 0 没有
例如: select round(rand()*100,0)
sqrt(4); 平方根
truncate(x,y); 保留几位小数 , 不四舍五入
聚合函数:
max(), min() , avg() , sum() , count(),
group_concat(ename); 将指定字段的值 拼成一条记录 用,隔开
字符串函数:
ASCII('A'); 返回当前字符的ASCII码值
concat(value1,value2); 字符串拼接
CONCAT_WS(sep,s1,s2...,sn); 指定字符拼接字符串
INSERT(str,x,y,instr); 插入并替换 从 x 位置开始 1 ,
LOWER(str); 将字符变小写
upper(str); 将字符变大写
length(x); 返回当前字符串的长度
ltrim(); 去左边空格
rtrim(); 去右边空格
trim(); 去掉两边空格
POSITION('el' IN 'hello'); 返回el 在hello 字符串中 出现的位置
REVERSE(str);反向符串输出
left(str,x); right(str,x);
***字符串截取:
select SUBSTR('hello',3,2);
select SUBSTR('hello',3); -- 截取到最后
***字符串替换:
select REPLACE('hello','el','haha');
--- 将emp 表中的 ename 字段的 首字母大写
例如: Ford
日期函数:
CURDATE(); 返回当前系统日期
CURRENT_DATE(); 返回当前系统日期
now(); 返回当前系统时间 + 时分秒
DATE_ADD(CURDATE(),INTERVAL 20 day);
相当于将系统时间 + 20 天 ,
DATE_FORMAT(now(),'%y_%m_%d %H_%i_%s'); 日期格式化 i 表示分钟
****: 当前员工的入职时间:
PERIOD_DIFF
案例:
select PERIOD_DIFF(DATE_FORMAT(CURDATE(),'%Y%m')
,DATE_FORMAT(HIREDATE,'%Y%m'))/12
from emp where ename = 'allen';
流程控制函数:
IF(test,t,f); test 值如果为true ,输出t ,否则输出f
IFNULL(comm,'没有佣金'); 如果comm 时空 就返回 值, 否则
返回自己;
case when:
1. case 字段名 when 值 then 结果1 else 结果2 end
如果这该字段名的 是 when 的值, 那就输出值, else
if(){
}else{
}
2. select `name`,
case when age <= 8 then '幼儿'
when age >= 9 and age <= 18 then '少年'
when age >=19 and age <=35 then '中年'
when age >=36 and age <=65 then '老年' end
from `user`;
0-8 幼儿
8-18 少年
18-35 青年
35-50 中年
50-65 老年
if(){
}else if(){
}else if(){
}
加密函数了解:
登录操作 都要加密:
MD5(str) 将字符串转换为 长度为32位的字符串 ,不可逆
insert into `user`(uid,uanme,`password`)
values(null,'李四',MD5('123'));
show VARIABLES like '%char%'; 查看mysql 数据库管理系统的字符集