随笔 - 95,  文章 - 0,  评论 - 0,  阅读 - 14111
修改表
alter table 表名 add 字段名 类型(长度) [约束]; -- 添加列 alter table 表名 modify 字段名 类型(长度) [约束]; -- 修改列的类型长度及约束 alter table 表名 change 旧字段名 新字段名 类型(长度) [约束]; -- 修改列表名 alter table 表名 drop 字段名; -- 删除列 alter table 表名 character set 字符集; -- 修改表的字符集 rename table 表名 to 新表名; -- 修改表名
增加数据
insert
into 表(字段名1,字段名2..) values(值1,值2..);-- 向表中插入某些列 insert intovalues(值1,值2,值3..); -- 向表中插入所有列
修改数据
update
表名 set 字段名=值,字段名=值...; -- 这个会修改所有的数据,把一列的值都变了 update 表名 set 字段名=值,字段名=值... where 条件; -- 只改符合where条件的行
删除数据
delete
from 表名 -- 删除表中所有记录 delete from 表名 where 条件 -- 删除符合 where条件的数据 truncate table 表名; -- 把表直接drop掉,重新建表,auto_increment将置为零。删除的数据不能找回。执行速度比delete快
查询数据
select
* from 表名; -- 查询所有列 select 字段名1,字段名2,字段名3.. from 表名; -- 查询指定列
复制代码
基本sql
select
* from 表名 where 范围 -- 选择查询 insert into 表名(field1,field2) values(value1,value2) -- 插入 delete from 表名 where 范围 -- 删除 update 表名 set field1=value1 where 范围 -- 更新 select * from 表名 where field1 like%value1%-- 查找 select * from 表名 order by field1,field2 [desc] -- 排序: select count as 需要统计总数的字段名 from 表名 -- 总数 select sum(field1) as sumvalue from 表名 -- 求和 select avg(field1) as avgvalue from 表名 -- 平均 select max(field1) as maxvalue from 表名 -- 最大 select min(field1) as minvalue from 表名 -- 最小
复制代码
复制代码
like的用法
A:% 包含零个或多个字符的任意字符串: 1like'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden)。 2like'%inger' 将搜索以字母 inger 结尾的所有字符串(如 Ringer、Stringer)。 3like'%en%' 将搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。 B:_(下划线) 任何单个字符: like'_heryl' 将搜索以字母 heryl 结尾的所有六个字母的名称(如 Cheryl、Sheryl)。 C:[ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符: 1like'[CK]ars[eo]n' 将搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。 2like'[M-Z]inger' 将搜索以字符串 inger 结尾、以从 M 到 Z 的任何单个字母开头的所有名称(如 Ringer)。 D:[^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符: like'M[^c]%' 将搜索以字母 M 开头,并且第二个字母不是 c 的所有名称(如MacFeather)。 E:* 它同于DOS命令中的通配符,代表多个字符: c*c代表cc,cBc,cbc,cabdfec等多个字符。 F:?同于DOS命令中的?通配符,代表单个字符 : b?b代表brb,bFb等 G:# 大致同上,不同的是代只能代表单个数字。k#k代表k1k,k8k,k0k 。 下面我们来举例说明一下: 例1,查询name字段中包含有“明”字的。 select * from table1 where name like '%明%' 例2,查询name字段中以“李”字开头。 select * from table1 where name like '李*' 例3,查询name字段中含有数字的。 select * from table1 where name like '%[0-9]%' 例4,查询name字段中含有小写字母的。 select * from table1 where name like '%[a-z]%' 例5,查询name字段中不含有数字的。 select * from table1 where name like '%[!0-9]%' 以上例子能列出什么值来显而易见。但在这里,我们着重要说明的是通配符“*”与“%”的区别。 很多朋友会问,为什么我在以上查询时有个别的表示所有字符的时候用"%"而不用“*”?先看看下面的例子能分别出现什么结果: select * from table1 where name like '*明*' select * from table1 where name like '%明%' 大家会看到,前一条语句列出来的是所有的记录,而后一条记录列出来的是name字段中含有“明”的记录,
所以说,当我们作字符型字段包含一个子串的查询时最好采用“
%”而不用“*”,用“*”的时候只在开头或者只在结尾时,而不能两端全由“*”代替任意字符的情况下。
复制代码
复制代码
单表查询

#----综合使用 书写顺序 select distinct * from '表名' where '限制条件' group by '分组依据' having '过滤条件' order by limit '展示条数' 执行顺序 from -- 查询 where -- 限制条件 group by -- 分组 having -- 过滤条件 order by -- 排序 limit -- 展示条数 distinct -- 去重 select -- 查询的结果 正则:select * from emp where name regexp '^j.*(n|y)$'; 集合查询:maxminavgsumcount 、group_concat 。 拼接:concat、concat_ws、group_concat 内连接:inner join 左连接:left join 右连接:right join 全连接: 左连接 union 右连接
复制代码
复制代码
where查询
# between 在...之间 select id,name from emp where id >= 3 and id <= 6; 相当于: select * from emp where id between 3 and 6; # or 或者 select * from emp where id >= 3 or id <= 6; # in,后面可以跟多个值 select * from 表名 where 字段名 in (条件1,条件2,条件三); # like (见上18) # char——length() 取字符长度 select * from 表名 where char_length(需要获取长度的字段名) = 4; not 配合使用 注意:判断空不能用 = ,只能用 is
复制代码
group by分组
select
查询字段1,查询字段2,... from 表名 where 过滤条件 group by分组依据 # 分组后取出的是每个组的第一条数据
复制代码
聚合查询
# max 最大值 # 每个部门的最高工资 select post,max(salary) from emp group by post; # min 最小值 # 每个部门的最低工资 select post,min(salary) from emp group by post; # avg 平均值 # 每个部门的平均工资 select post,avg(salary) from emp group by post; # 每个部门的工资总和 # sum 求和 select post,sum(salary) from emp group by post; # count(需要计数字段) 计数 # 每个部门的人数 select post,count(id) from emp group by post; # group_concat(需要分组后的字段) # 不仅可以用来显示除分组外字段还有拼接字符串的作用 select post,group_concat(name) from emp group by post; -- post:分组字段,name 需要分组后显示的字段 拼接: concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用 举例: select concat("NAME: ",name) as 姓名 from emp; concat_ws: 如果拼接的符号是统一的可以用 举例: select concat_ws(':',name,age,sex) as info from emp; group_concat: 举例: select post,group_concat(name,'DSB') from emp group by post; 补充:as语法 起别名 select name as 姓名,salary as 薪资 from emp;
复制代码
order by 排序
select
* from emp order by salary asc; #默认升序排 select * from emp order by salary desc; #降序排 # 多条件排序 #先按照age降序排,在年轻相同的情况下再按照薪资升序排 select * from emp order by age desc,salary asc;
复制代码
limit 查询条数
# 限制展示条数
select * from emp limit 3; # 查询工资最高的人的详细信息 select * from emp order by salary desc limit 1; # 分页显示 select * from emp limit 0,5; # 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置 select * from emp limit 5,5;
复制代码
复制代码
表连接
1
、内连接:只取两张表有对应关系的记录(只拼两个表共有的) 左表 inner join 右表 on 条件 select * from emp inner join dep on emp.dep_id = dep.id where dep.name = "技术"; 2、左连接:在内连接的基础上,保留左边的数据,右边没有就为空 左表 inner left 右表 on 条件 3、右连接:在内连接的基础上,保留右边的数据,左边没有就为空 左表 inner right 右表 on 条件 4、全连接:左右连接都有,用union连接 左表 inner left 右表 on 条件 union 左表 inner right 右表 on 条件 select * from emp left join dep on emp.dep_id = dep.id union select * from emp right join dep on emp.dep_id = dep.id;
复制代码

 

posted on   天涯何  阅读(52)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示