成都10班-蒲琳韦 发表于 2021-11-22 20:16:59

mysql语句

本帖最后由 成都10班-蒲琳韦 于 2021-11-22 20:54 编辑

查询语句
1.select * from +表名;查询表中所有的字段
2.select字段 from +表名;查询表中某字段的内容
3.select 字段,字段 from +表名; 查询表中某两个字段的内容
4.select * from +表名 where 字段=某个值;查询表中满足某个条件的所有数据
5.select * from +表名 where 字段!=某个值;查询表中不满足某个条件的所有数据,‘!=’也可以用<>代替,表示不等于
6.select * from +表名 where 条件1and 条件2;查询表中同时满足多个条件的数据
7.select * from +表名 where 条件1or 条件2;查询表中至少满足1个条件的数据
8.select * from +表名 where 字段 between ...and...;查询一个条件范围内的数据
9.select * from +表名 where 字段 in(值1,值2,值3);查询字段满足在指定的集合中的数据
10.select * from +表名 where 字段 not in(值1,值2,值3);查
询字段不满足在指定的集合中的数据
11.select * from +表名 where 字段 is null;查询字段值为空的数据,不为空是is not null
12.select * from +表名 where 字段like '%值%’;查询某个字段模糊匹配成功的数据,%用于匹配字段的开头和结尾
13.select * from +表名 where 字段limitm n; 查询限定数量的数据,m表示下标,n指限定的数量,下标我m开始的你条数据,下标开始是0不是1
14.select * from +表名 order by 字段 asc;查询的数据根据某个字段从小到大排序
15.select * from +表名 order by 字段 desc;查询的数据根据某个字段从大到小排序
16.select * from +表名 group by 字段;查询的数据根据某个字段进行分组
17.select * from +表名 group by 字段 having 条件;查询的数据根据某个字段进行分组再条件过滤,having在group by后面相当于where
注意:where不能放在group by后面

mysql增删改语句
1.insert into +表名 values(字段1值,字段2值,字段3值...);给表中插入数据
2.insert into +表名 values(字段1值,字段2值,字段3值...),(字段1,字段2,字段3...);给表中插入多条数据
3.insert into +表名 (字段1,字段2,字段3)values(字段1值,字段2值,字段3值...);给表中指定字段插入数据
4.delete from +表名 where 条件;删除表中指定数据,不释放空间
5.truncate+表名;删除表中所有数据,表的结构还在,释放空间
6.drop table +表名 ;删除表,若表中有数据也一并删除了,释放空间
7.update+表名 set 字段名=值 where 条件;更新表中指定字段数据,即更改

备份
1.create table +表1 like +表2;备份表,创建一个表与某个表相同
2.insert into +表1 select * from 表2;备份数据,把表2的所有数据插入到表1
3..insert into +表1(字段1,字段2) select字段1,字段2 from 表2;把表2的字段1和2的数据插入到表1
4.mysqldump -uroot -p 数据库名>脚本名   备份数据库
5.mysql -uroot -p +数据库<脚本名还原数据库
备份和还原数据库要退出mysql,在linux系统中进行
聚合函数
1.select count(*)from +表名;统计查询数据的数量
2。select sum(字段)from +表名;查询某个字段求和
3.select max(字段)from +表名;查询某个字段的最大值
4.select min(字段)from +表名;查询某个字段的最小值
5.select avg(字段)from +表名;查询某个字段的平均值
6.select distinct(字段) from +表名;对某个字段去重



页: [1]
查看完整版本: mysql语句