MySQL基础之DML、DQL

一、DML(data manipulation language) 

DML是数据操作语言,用于增删改数据库。

选择数据:select

修改数据:update

删除数据:delete

增加数据:insert

DML添加数据:

1、给指定字段添加数据
insert into 表名(字段名1,字段名2...) values(值1,值2,...);

---字符串和日期型数据要加''引号---
---插入的数据大小,应该在字段的规定范围内---
2、给全部字段添加数据
insert into 表名 values(值1,值2...);
3、批量添加数据
insert into 表名(字段名1,字段名2...)values(值1,值2,...),(值1,值2,...)(值1, 值2,...)
insert into 表名 values(值1,值2,...),(值1,值2,...);

DML修改数据:

update 表名 set字段名1=值1,字段名2=值2,...[where条件];

---修改语句的条件可以有,也可以没有。如果没有条件,则会修改整张表的所有数据---

DML删除数据:

delete from 表名 [where条件]
---delete语句不能删除某一个字段的值(可以使用update)---

二、DQL(Date Query Language)

DQL是数据查询语言,查询数据库表记录。

SELECT 字段列表
FROM 表名列表
WHERE 条件列表
GROUP BY 分组字段列表
HAVING 分组后条件列表
ORDER BY 排序字段列表
LIMIT 分页参数

条件查询(where)
聚合函数(count,max,min,avg,sum)
分组查询(group by)
排序查询(order by)
分页查询(limit)
举例如下:
----1、查询指定字段name,age,并返回表teachers_table----
select name,age from teachers_table;

---2、查询所有字段,写法1----
select id, name,gender,age,idcard from teachers_table;

---查询所有字段,写法1----
select * from teachers_table;

---3、查询所有教师的身份证号---
select idcard from teachers_table;
---或者---
select idcard as '身份证' from teachers_table;
---或者---
select idcard  '身份证' from teachers_table;


---4、查询所有教师的姓名,且不重复---
select distinct name '姓名' from teachers_table;
 DQL条件 查询
 select 字段列表 from 表名 where 条件列表;

 运算符
比较运算符说明逻辑运算符说明
=等于NOT 或 !非,不是
>大于OR 或者(多个条件任意一个成立)
>=大于等于AND 或 &&并且(多个条件同时成立)
<小于
<=小于等于
!=或者<>不等于
IN(...)在in之后的列表中的值,多选...
BETWEEN...AND...在某个范围之内的值,多个选一个
LIKE 

占位符,模糊匹配。

- 匹配单个字符,%匹配任意字符

IS NULL是NULL

举例如下:

---1、查询分数等于90的学生---
select * from table_students where score = 90;

---2、查询分数大于60的学生---
select * from table_students where score > 60;

---3、查询姓名是三个字的同学信息--
select * from table_students where name like '__';

---4、查询没有分数的学生---
select * from table_students where score is null;


---5、查询有分数的学生---
select * from table_students where score is  not null;

---6、查询分数不等于92分的学生---
select * from table_students where score != 92;
--或者--
select * from table_students where score <> 92;

---7、查询分数包含85到100之间的学生---
select * from table_students where score >= 85 and score <= 100;
--或者--
select * from table_students where score >= 85 && score <= 100;
--或者--
select * from table_students where score between 85 and 100;

---8、查询分数等于 85 或 90 或 95 的同学信息---
select * from table_students where score = 85 or score = 90 or score = 95;
--或者--
select * from table_students where score in(85,90,95);

---9、查询女生分数大于80的学生---
select * from table_students where gender = '女‘ and score > 80;

---10、查询身份证最后一位是6的学生---
select * from table_students where like '%6';
select * from table_students where like '_________________6';





  • 28
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值