当前位置:网站首页>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;
-- 对总分按照降序进行排列
边栏推荐
猜你喜欢
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
我走過最迷的路,是字節跳動程序員的腦回路
Notes on how the network is connected
汇编课后作业
The QT program compiled on CentOS lacks a MySQL driven solution
Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.
Flink源码解读(三):ExecutionGraph源码解读
Full record of ByteDance technology newcomer training: a guide to the new growth of school recruitment
Programmer orientation problem solving methodology
MySQL digital function
随机推荐
唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
汇编课后作业
数据传送指令
was unable to send heartbeat
MySQL字符串函数
8086 内存
Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」
Activiti目录(五)驳回、重新发起、取消流程
Typescript basic operations
Idea resolving jar package conflicts
Ruoyi-Cloud 踩坑的BUG
The difference between URI and URL
算数运算指令
IDEA断点调试技巧,多张动图包教包会。
原型链继承
When it comes to Google i/o, this is how ByteDance is applied to flutter
8086 CPU internal structure
Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
唯有学C不负众望 TOP2 p1变量