数据库常用命令(二)

插入,修改,更改语句
1、insret into 表名[(字段列表)] value(值列表); //插入单条数据
2、insert into 表名[(字段列表)] values(值列表1),(值列表2),(......),(值列表n); //插入多条语句
3、replace into 表名[(字段列表)] values(值列表); //使用replace语句插入单条数据
4、replace into 表名[(字段列表)] values(值列表1),(值列表2),(......),(值列表n); //使用replace插入多条语句
5、insert into 目标数据表(字段列表1) select 字段列表2 from 源数据表 where 条件表达式; //将一个表中查询出来的数据插入到另一个表中
6、insert into 表名 set 字段名1=值1,字段名2=值2; //插入数据
7、update 表名 set 字段名1=值1,字段名2=取值2,…,字段名n=取值n [where 条件表达式]; //更新数据表中数据
8、delete from 表名 [where 条件表达式]; //删除数据
9、truncate [table] 表名; //无条件删除表

查询操作
10、select * from 表名; //查询表中所有属性
11、select 列名1,列名2,....列名n from 表名; //查询表中指定列
12、select 列名1,列名2,....列名n from 表名 where 条件; //选择行查询
13、select 列名1,列名2,....列名n from 表名 where [not] 表达式1 逻辑运算符 表达式2; //使用and,or,not三种运算符查询
14、select 列名1,列名2,....列名n from 表名 where 表达式 [not] between 初始值 and 终止值; //使用BETWEEN AND来限制查询数据的范围
15、select 列名1,列名2,....列名n from 表名 where 表达式 [not] in(值1,值2,....值n); //使用in限制查询数据的范围
16、select 列名1,列名2,....列名n from 表名 where 列名 [not] like '字符串' [escape '转义字符']; //使用like进行模糊查询
17、select 列名1,列名2,....列名n from 表名 where 列名 is [not] null; //查询表信息为空的或不为空的列
18、select distinct 列名1,列名2,....列名n from 表名 where 条件; //消除重复结果集
19、select 列名1,列名2,....列名n from 表名 where 条件 order by 列名x asc; //按列名x升序排列
20、select 列名1,列名2,....列名n from 表名 where 条件 order by 列名y desc; //按列名y降序排列
21、select 列名1,列名2,....列名n from 表名 where 条件 order by 列名x asc 列名y desc; //先按列名x升序排列再按列名y降序排列
22、select 列名1,列名2,....列名n from 表名 limit offset; //offset为可选项,默认为0,当为1时查询从第二条开始,依次类推
23、select sum(列名) from 表名; //统计总数
24、select count(列名) from 表名; //统计个数
25、select max(列名) from 表名; //返回最大值
26、select min(列名) from 表名; //返回最小值
27、select avg(列名) from 表名; //返回各值的平均值
28、select 列名1,列名2,....列名n from 表名 group by 字段名; //分组统计
29、select 列名1,[sum][max][min][avg]count(列名) from 表名 group by 列名; //group by和聚合函数一起用
30、select 列名 form 表名 where 列名=(select 列名 from 表名 where 条件表达式); //子查询
31、select 列名 form 表名 where 列名 in(select 列名 from 表名 where 条件表达式); //in字查询
32、select 列名1,列名2 from 表名 where 条件表达式 union select 列名1,列名2 from 表名 where 条件表达式; //联合查询
33、select u.列名1,s.列名2 from 表名1 u join 表名2 s on u.列名x=s.列名x [where][group by]; //内连接,两表中读存在列名x
34、select u.列名1,s.列名2 from 表名1 u left join 表名2 s on u.列名x=s.列名x [where] [group by]; //左外连接,两表中读存在列名x
35、select u.列名1,s.列名2 from 表名1 u right join 表名2 s on u.列名x=s.列名x [where] [group by]; //右外连接。两表中读存在列名x

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容