数据库
首页 > 数据库> > MySQL数据库从小白到小菜02

MySQL数据库从小白到小菜02

作者:互联网

MySQL数据库从小白到小菜02

在表中的相关操作

crud:增查改删;增加(create)、查找(retrieve)、修改,更新(update)、删除(delete)。
SQL语言中,单引号(双引号)用来表示字符串

增加

(此时是在lit库中staff表中的操作,其中staff表有4个属性)

--单行数据插入
INSERT INTO staff VALUES (1, '唐三藏222', 987,'987');
--如果插入中文时出错,则应检查MySQL的字符集

--多行数据插入
 INSERT INTO staff VALUES 
(1, '唐三藏', 18,'hello'),
(2, '123',17,'world');

--指定列数据插入,其他列为默认值(后面会提到)
INSERT INTO staff (name, age) VALUES
 ('曹孟德',111),
 ('孙仲谋',123);

在插入日期时(包括建表):

create table time(id int ,time datetime);
--now()是sql中的内置函数
insert time values(1,now());
--插入任意时间
insert time values(2,'2021-06-10 12:22:22');
--插入日期,默认时间为00:00:00
insert time values(3,'2021-06-10');

查找(指定列查找、列运算、别名、去重、排序)

--查询全部数据(全列查找)
select * from staff;

--查询指定列数据,语句中前后顺序会影响结果表中的前后顺序
select id,name,age,com from staff;

--指定列时可以让某一列进行一定的运算
select id,name,age+18,com from staff;

--也可以对某几列进行运算,此时有可能两个列所存储的类型不同,则会尽可能的保证数据结果正确。
Select id+age,name from staff;

--起别名,用as来起别名,as可以省略,但是为了增加代码的可读性,一般不会省略
select name,comment as 备注 from staff;

--去重distinct,根据查询结果中某几列的值进行去重,此处为id这一列进行去重,也可以对多列进行去重。
select distinct id from staff;

--以某一列进行排序,此处为根据age进行排序默认为升序,如果需要降序则加desc
select * from staff order by age ;
select * from staff order by age desc;

--以多列进行排序,如果第一列排序时两个值相同,则按第二列进行排序依次类推
select * from staff order by age , id;
select * from staff order by age desc,id;
select * from staff order by age desc,id desc
模糊查找

模糊查找:
ps:%可以代替多个字符
而_只代替一个字符

--查找name以唐开头的
select * from staff where name like '唐%';

--查找name以1结尾的
select * from staff where name like '%1';

--查找name中包含1的
select * from staff where name like '%唐%';

--查找name以唐开头且只有两个字符的
select * from staff where name like '唐_';
日期查询

查找两日期之间:

--创建一个article文章表并添加数据
create table article(name varchar(20),create_time datetime);
insert article values('qqq',now());
insert article values('111111','2019-06-11 15:29:00'),('222222','2019-03-11 15:29:00'),('3333','2019-11-11 15:29:00'),
('444','2019-10-11 15:29:00'),('555','2019-11-10 16:02:00'),('666','2021-11-11 15:29:00');

-- 查询article文章表中,发表日期create_date在2019年1月1日上午10点30分至2019年11月10日下午4点2分的文章
select * from article where create_time between '2019-01-01 10:30:00' and '2019-11-10 16:02:00';

查找某日期至今:

--article表中添加数据
insert article(create_time) values('2018-01-01 10:30:00'),('2019-01-01 10:30:00'),('2020-01-01 10:30:00'),('2015-01-01 10:30:00');

-- 查询article文章表中,文章标题title为空,或者满足发表日期create_date在2019年1月1日之后
select * from article where name is null or create_time between '2019-01-01 10:30:00' and now();

查询包括今天之后的:

-- 查询在19-1-1之后的日期
select * from article where name is null or create_time > '2019-01-01 00:00:00');
空查询

如果用等号,则判断null=null得到的结果为null不是true或false,所以此处不能用等号,要想正确查找空值记录,要么使用is,要么使用<=>。

--查找name为null的
select * from staff where name is null;

--查找name为null的
select * from staff where name <=> null;

--查找name不为null的
select * from staff where name is not null;
条件查询

在where语句中不能使用别名

--查询年龄小于18的
select * from staff where age < 18;

--查询年龄大于18且小于25的
select * from staff where age > 18 and age < 25;
select * from staff where age between 18 and 25;

--查询年龄大于25或年龄小于18的
select * from staff where age > 25 or age < 18;
select * from staff where age not between 18 and 25;

--查询年龄加备注小于500的,此处可以看到 int类型加varchar类型也可以跟int进行比较,则此处发生了自动转换。
select * from staff where age+comment<500;

--查询年龄时18或15的
SELECT * from staff where age =18 or age=15;

--如果需要定义优先级,则带括号
select * from staff where age = 18 or (age < 15 and age > 5 );

--列举查询,查询年龄是5,17,19,28的
select * from staff where age in (5,17,19,28);
分页查询

如果数据库表里面的数据比较多,一个不加限制的查找时间可能会比较久,甚至可能导致数据库卡死,所以对这些操作做出一些限制,如:条件查询,分页查询也是一种限制,可以搭配查询的任意功能。

--直接使用limit,限制结果不超过3条,其余的不要了
select * from staff limit 3;

--查询成绩前三的学生姓名及总分(需要对成绩总和排序)
select name,chinese+english+math from exam_result order by chinese+english+math desc limit 3;

--查询成绩第4-第6这三名学生的信息(offset为从第几个开始查询,默认为0,查第四名则从第三个开始查)
--所以时通过offset来确定起始位置,通过limit来确定结果长度。
select name,chinese+english+math from exam_result order by chinese+english+math desc limit 3 offset 3;

--也可以直接用limit直接确定起始位置和结果长度,起到和上面查询一样的效果,表示从0位置开始取3个结果
select name,chinese+english+math from exam_result order by chinese+english+math desc limit 0,3;

修改(更新)

修改,修改数据库中的数据,也可以理解为更新数据库中的数据。

--把name为123的数学成绩修改为88,由于name类型为varchar,则需加单引号
update exam_result set math = 88 where name = '123';

--把id为2的数学成绩修改为99,此处如果有多条id为2的数据,则会修改多条数据
update exam_result set math = 99 where id = 2;

--把姓名为123的同学数学与语文成绩修改为83
--修改多列数据
update exam_result set math = 83, chinese = 83 where name = '123';

--把总成绩为倒数前三的同学的数学成绩加上5分
update exam_result set math = math+5 order by chinese+math+english limit 3;

删除

删除与修改一样,后面where的条件决定要操作的数据是一条还是多条

--删除姓名为123的信息
delete from exam_result where name = '123';

--删除所有英语不及格的信息
delete from exam_result where english < 60;

--删除表中所有信息
--当删除数据时不加任何条件,则删除表中所有数据,但不删除表,表的结构也在
delete from staf;

标签:02,00,白到,--,name,MySQL,where,select,staff
来源: https://blog.csdn.net/weixin_52793965/article/details/117716165