一口气看完MySQL--下篇

一口气看完MySQL--下篇

  • 前言
  • 数据的操作
    • 增 insert
    • 删 delete
    • 改 update
    • 查 select
      • 无条件的查询
      • 有条件的查询
        • 1.比较运算符条件
        • 2.逻辑运算符条件
        • 3.范围搜索条件
        • 4.列表搜索条件
        • 5.like关键字搜索
        • 6.空值查询操作
    • 查询结果分组 group by
    • 查询结果排序 order by
    • 聚合函数
    • 四类函数
      • 字符串函数
      • 数学函数
      • 日期时间函数
      • 控制流函数


前言

本文衔接上文:一口气看完MySQL–上篇
上篇记录了: MySQL的介绍
      数据库的操作
      表的操作


以下是本篇文章正文内容

数据的操作

包括三条DML语句 INSERTUPDATEDELETE
一条SQL语句 SELECT

增 insert

如果插入的值是字符串类型或者时间日期类型,需要 单引号

1.格式一
insert into 表名(列名1,列名2,列名3) values(1,2,3);

2.格式二
insert into 表名 values(1,2,3);

3.格式三
insert into 表名 values(1,2,3),(4,5,6);

4.格式四
insert into 表名 set 列名1 =1,列名2 =2,列名3 =3;




删 delete

 删除数据时,要考虑外键的影响

1.格式一	-- 删除表中所有数据
delete from 表名;

2.格式二
delete from 表名 where 删除条件;
	2.1多个条件同时满足
	delete from 表名 where 条件1 and 条件2...;
	
	2.2多个条件满足一个
	delete from 表名 where 条件1 or 条件2...;




改 update

1.格式一
update 表名 set 列名=;

2.格式二
update 表名 set 列名1=1,列名2=2...;

3.格式三	有条件的更新
update 表名 set 列名1=1,列名2=2... where 条件;
-- update student set age=age+1 where sex='男';




查 select

查询语句的顺序
select 列名|*
 from 表名
 [where 查询条件(不能是聚合函数)]
 [group by 分组依据列 [having 分组条件]]
 [order by 排序依据列 [asc/desc]]


注: []表示可有可无,
  asc升序 desc降序排列

无条件的查询

select * from 表名;
-- select子句	查询哪些列的内容
-- from子句	从哪个表中查数据

2.去重查询
select distinct 列名 from 表名;
-- select distinct 籍贯 from student;

3.别名查询
-- 针对查询的结果,另起标题名字
	3.1格式一
	select 列名1 '别名1',列名2 '别名2'... from 表名;
	
	3.2格式二
	select 列名1 as '别名1',列名2 as '别名2'... from 表名;
	
4.在指定行数查看	limit
select * from 表名 limit [开始的位置(可不写)],查询的行数;
-- 开始位置数字=行数位置-1




有条件的查询

1.比较运算符条件

运算符 含义
= 等于
> 大于
< 小于
>= 大于等于
<= 小于等于
<> 不等于
!= 不等于
select * from 表名 where 列名 !=;
-- select * from student where sex != "男";

2.逻辑运算符条件

运算符 含义
and 连接多个条件,同时满足
or 连接多个条件,满足其中一个
select * from 表名 where 列名1 !=1 and 列名2 >2;
-- select * from student where sex != "男" and id > 5;

3.范围搜索条件

运算符 含义
between 开始值 and 结束值 在范围内
not between 开始值 and 结束值 不在范围内
select * from 表名 where 列名1 between1 and2;
-- select * from student where id between 2 and 5;

4.列表搜索条件

运算符 含义
in (值1,值2,值3) 在给定值中
not in (值1,值2,值3) 不在给定值中
select * from 表名 where 列名1 in (1,2,3) ;
-- select * from student where id in (2,3,4,5);

5.like关键字搜索

通配符 含义
% 表示0个或多个字符
_ 表示一个字符
5.1要模板相关数据
select * from 表名 where 列名 like '字符模板';

5.2不要模板相关数据
select * from 表名 where 列名 not like '字符模板';
  
字符模板:王%		王_

6.空值查询操作

is null 为空
is not null 不为空
6.1为空
select * from 表名 where 列名 is null;

6.2不为空
select * from 表名 where 列名 is not null;




查询结果分组 group by

针对查询出来的结果,按照某个列来进行划分组

1.无条件
select 列名,聚合函数 from 表名 group by 列名;
-- 每个专业多少学生
-- select 专业,count(*) '人数'
-- from 学生表
-- group by 专业;

2.有条件
select 列名,聚合函数 from 表名 group by 列名 having 分组条件;
-- 学生超过十人的专业
-- select 专业,count(*) '人数'
-- from 学生表
-- group by 专业;
-- having count(*)>10




查询结果排序 order by

针对查询出来的结果, 按照某个列进行排序操作

select * from 表名 order by 列名;

1.升序	默认
	select * from 表名 order by 列名 asc;

2.降序
	select * from 表名 order by 列名 desc;

3.二级排序
	select * from 表名 order by 列名1 desc,列名2 asc;




聚合函数

特别强调 where后面不能写聚合函数

-- sum(列名)	avg(列名)	max(列名)	min(列名)
   select sum(列名) '总和' from 表名;
   select avg(列名) '平均' from 表名;
   select max(列名) '最大' from 表名;
   select min(列名) '最小' from 表名;

特别强调 除了count(*)外,其他聚合函数都忽略空值

--	统计元组的个数
    select count(*) '元组数' from 表名;

--	统计该列值的个数
    select count(列名) '值的个数' from 表名;
    
-- 	去重统计
  	select count(distinct 列名) '去重个数' from 表名

四类函数

字符串函数

char_length 字符串字符长度
length 字符串字节长度
mid 字符串截取
1.字符长度
select 列名,char_length(列名) '字符长度' from 表名;

2.字节长度
-- 一个汉字占3个字节
select 列名,length(列名) '字节长度' from 表名;

3.字符串截取
-- mid(操作的字符串,截取的开始位置,截取字符长度)
select 列名,mid(列名,2,2) '截取' from 表名;

数学函数

round 四舍五入
least 取最小数字
greatest 取最大数字
1.1保留整数    -- round(数字)	其实是round(数字,0)
	-- 平均成绩四舍五入
	select round(avg(grade)) '平均成绩' from student;

1.2保留小数    -- round(数字,保留小数位)
	select round(3345.26723,2);
	--	结果是3345.27

日期时间函数

now 数据库服务器当前日期时间
current_date 数据库服务器当前日期
current_time 数据库服务器当前时间
- -
to_days 日期转换成天数
dayofyear 该年已过的天数
week 当前是第几周

控制流函数

1.if(布尔表达式,参数1,参数2);
-- select if(条件,"男","女") from student;

2.ifnull(参数1,参数2)
--	参数一不为空则返回参数一,否则返回参数二
3.if 条件 then 执行语句
  elseif 条件 then 执行语句
  else 执行语句 end if

一口气看完MySQL–下篇 详细的数据的操作到这里就结束了
有机会再写个补充篇记录表连接和多表查询操作
感谢大家的支持!!!

你可能感兴趣的:(MySql,mysql,数据库,sql)