一口气看完MySQL--下篇


前言

本文衔接上文:一口气看完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–下篇 详细的数据的操作到这里就结束了
有机会再写个补充篇记录表连接和多表查询操作
感谢大家的支持!!!

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值