当前位置:网站首页>mysql 基本增删改查SQL语句
mysql 基本增删改查SQL语句
2022-07-06 09:32:00 【萌新菜狗】
CRUD
Insert语句
insert into `good` (id,good_name,price) values(11,'苹果手机',5000);
-- 表名 列名 列名 列名 值
- insert语句注意事项
- 插入的数据应与字段的数据类型相同
- 数据的长度应列的规定范围内,例如:不能将一个长度80的字符串加入到长度为40的列中
- 在values中列出的数据位置必须与被加入的列的排列位置相对应
- 字符和日期类型数据应包含在单引号中
- 列可以插入空值【前提是该字段允许为空】
- insert into tab_name (列名…) values(),(),()…;
- 如果是给表中的所有字段添加数据的时候,可以不写前面的字段名字
- 默认值的使用,当不给某个字段值时,如果有默认值就会添加,否则报错。
update语句
update good set price = price + 1000 where good_name = '苹果手机';
- 使用细节
- update语法可以用新值更新原有表行中的各列。
- set子句指示要修改哪些列和要赋给予哪些值。
- where子句指定应更新哪些行,如没有where子句,则更新所有的行(记录)
- 如果需要修改多个字段,可以通过set 字段1 = 值1,字段2 = 值2…
delete语句
delete from good where good_name = '苹果手机';
- 使用细节
- 如果不适用where子句,将删除表中的所有数据
- Delete语句不能删除某一列的值(可使用update设为null或者‘’)
- 使用delete语句仅删除记录,不删除表本身。如果要删除表,使用drop table 语句。
select语句
基本语法
select [distinct] *|{column1,column2,....} from table_name;
- 注意事项
- select指定查询哪些列的数据
- column指定列名
- *号代表查询所有列
- from指定查询哪张表
- distinct可选,指查询结果的时候,是否去掉重复的数据
-- 统计每个学生的总分
select `name`,(chinese+english+math) from student;
-- 给每个学生的总成绩加10分
select `name`,(chinese+english+math+10) from student;
-- 使用别名表示学生的总成绩
select `name` as '名字',(chinese+english+math) as total_score from student;
where中经常使用的运算符
比较运算符
> < <= >= = <> != -- 大于,小于,大于(小于)等于,不等于
between ... and ... -- 显示在某一区域的值
in(set) -- 显示在in列表中的值,例如,in(100,200) 这个set是个集合,不是表示区间
like '张pattern' not like '' -- 模糊查询
is null -- 判断是否为空
逻辑运算符
and -- 多个条件同时成立 or -- 多个条件任一成立 not -- 不成立,例如:where not (salary>100);
select * from student where (chinese+english+math) > 200 and math< chinese and `name` like '韩%';
-- 这个 韩% 表示是以韩开头的字符串,无论韩后面有多少个字符都可以。
-- 韩_ 表示是以韩开头的字符串,但是 韩后面只能有一个字符。
- between …and…(闭区间)
select * from student where english between 80 and 90;
使用order by 子句排序查询结果
select column1,column2,... from tablename order by column asc|desc;
- order by 指定排序的列,排序的列既可以是表中的列名,也可以是select语句后指定的列名
- asc升序【默认】,desc降序
- order by 子句应位于select语句的结尾
select * from student order by math;-- 默认是升序
select * from student order by math desc -- 按照math成绩降序排列
select `name`,(chinese+math+english) as total_score from student order by total_score;
-- 对总分按照升序进行排列
select `name`,(chinese+math+english) as total_score from student order by total_score desc;
-- 对总分按照降序进行排列
边栏推荐
- 服务器端渲染(SSR)和客户端渲染(CSR)的区别
- 暑假刷题嗷嗷嗷嗷
- [graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
- Only learning C can live up to expectations TOP4 S1E6: data type
- Only learning C can live up to expectations top2 P1 variable
- Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
- Basic knowledge of assembly language
- 汇编语言段定义
- Programmer orientation problem solving methodology
- 唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
猜你喜欢
How to configure hosts when setting up Eureka
Coursera cannot play video
服务器端渲染(SSR)和客户端渲染(CSR)的区别
一个数10年工作经验的微服务架构老师的简历
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
Some instructions on whether to call destructor when QT window closes and application stops
Idea resolving jar package conflicts
Set up the flutter environment pit collection
汇编课后作业
JVM之垃圾回收器上篇
随机推荐
Log4j2 major vulnerabilities and Solutions
Programmer orientation problem solving methodology
Activiti目录(五)驳回、重新发起、取消流程
Activiti目录(四)查询代办/已办、审核
High performance mysql (Third Edition) notes
一个数10年工作经验的微服务架构老师的简历
原型链继承
MySQL optimization notes
算数运算指令
Ce n'est qu'en apprenant que c est à la hauteur des attentes Top5 s1e8 | s1e9: caractères et chaînes & opérateurs arithmétiques
The difference between URI and URL
J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
Activit零零碎碎要人命的坑
Koa Middleware
逻辑运算指令
"One year after graduation, I won ACL best paper"
关于Stream和Map的巧用
唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
Instructions for Redux
Which is more important for programming, practice or theory [there are some things recently, I don't have time to write an article, so I'll post an article on hydrology, and I'll fill in later]