DML 操作是指对数据库中表记录的操作,主要包括表记录的插入(insert)、更新(update)、删除(delete)和查询(select),是开发人员日常使用最频繁的操作。
插入记录(增 insert)
1 # 插入指定字段对应的内容 2 INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn); 3 4 # 表中有几个字段,就要写几个字段对应的内容 5 INSERT INTO tablename VALUES(value1,value2,……valuesn); 6 7 # 一次性插入多条数据 8 INSERT INTO tablename (field1, field2,……fieldn) VALUES (record1_value1, record1_value2,……record1_valuesn), (record2_value1, record2_value2,……record2_valuesn), …… (recordn_value1, recordn_value2,……recordn_valuesn) ;
删除记录(删 delete)
1 #清空表。原来有3条记录,下次再插入数据id 从4开始,除非指定id的值 2 delete from 表名 3 4 # 删除指定的内容 where 是条件 5 delete from 表名 where 字段=值; 6 7 # 下次插入数据 id下标就从1开始 8 truncate table 表名; 9 10 # 一次删除多个表的数据 11 DELETE t1,t2…tn FROM t1,t2…tn [WHERE CONDITION]
更新数据(改 update)
# 更改指定的内容 UPDATE tablename SET field1=value1,field2.=value2,……fieldn=valuen [WHERE CONDITION] # 同时更改多个表中的数据 UPDATE t1,t2…tn set t1.field1=expr1,tn.fieldn=exprn [WHERE CONDITION]
查询记录(查 select)
# 将表中的记录全部查询出来 select * from 表名; # 查看指定的字段 select 字段1,字段2 from 表名; # 查询单个字段不重复的记录 select distinct 字段 from 表名; # 根据指定的条件进行查询 select 字段 from 表名 where 字段=值;
注意: where 后面的条件可使用的运算符,除了‘=’外,还可以使用>、<、>=、<=、!=等比较运算符;多个条件之间还可以使用 or、and等逻辑运算符进行多条件联合查询
排序和限制
1 SELECT * FROM tablename [WHERE CONDITION] [ORDER BY field1 [DESC|ASC] , field2 [DESC|ASC],……fieldn [DESC|ASC]] 2 3 # 显示前5条数据 4 select * from money limit 5; 5 6 # 默认情况下,起始偏移量为0 7 select 字段 from 表名 limit [偏移量,数量]; 8 9 # 下标为2 从第三条记录开始 取5条结果 10 select * from money limit 2,5;
-
desc 降序
-
asc 升序
-
不写默认为升序排列
-
如果排序字段的值一样,则值相同的字段按照第二个排序字段进行排序
聚合操作
1 SELECT fun_name(field) FROM tablename [WHERE where_contition] [GROUP BY field1,field2,……fieldn [WITH ROLLUP]] [HAVING where_contition]
-
fun_name 表示聚合操作,也就是聚合函数,count( )统计总数,sum( )求和,avg( )求平均数,max( )求最大值,min( )求最小值。
-
GROUP BY 关键字表示要进行分类聚合的字段,比如要按照部门分类统计员工数量,部门 就应该写在 group by 后面。
-
WITH ROLLUP 是可选语法,表明是否对分类聚合后的结果进行再汇总。
-
HAVING 关键字表示对分类后的结果再进行条件的过滤。
1 mysql> select count(1) from emp; # 统计公司的总人数
+----------+
| count(1) |
+----------+
| 4 |
+----------+
`1 row in set (0.00 sec) 2 3 4 mysql> select deptno,count(1) from emp group by deptno; # 统计各个部门的人数 5 +--------+----------+ 6 | deptno | count(1) | 7 +--------+----------+ 8 | 1 | 2 | 9 | 2 | 1 | 10 | 4 | 1 | 11 +--------+----------+ 12 3 rows in set (0.00 sec) 13 14 15 mysql> select deptno,count(1) from emp group by deptno with rollup; # 既要统计各部门人数,又要统计总人数 16 +--------+----------+ 17 | deptno | count(1) | 18 +--------+----------+ 19 | 1 | 2 | 20 | 2 | 1 | 21 | 4 | 1 | 22 | NULL | 4 | 23 +--------+----------+ 4 rows in set (0.00 sec) 24 25 26 mysql> select deptno,count(1) from emp group by deptno having count(1)>1; # 统计人数大于1的部门 27 +--------+----------+ 28 | deptno | count(1) | 29 +--------+----------+ 30 | 1 | 2 | 31 +--------+----------+ 1 row in set (0.00 sec)
多表联合查询
-
内连接 inner join... on 仅选出两张表中互相匹配的记录
-
外连接 选出其他不匹配的记录
-
左连接 left join ... on
-
右连接 right join... on
-
子查询
# 内连接 mysql> select users.uid,users.username as ‘用户名‘,order_goods.name as ‘商品名‘,order_goods.buytime from users,order_goods where users.uid=order_goods.uid; +-----+-----------+------------+---------+ | uid | 用户名 | 商品名 | buytime | +-----+-----------+------------+---------+ | 2 | zhicheng | iPhone11 | 1111112 | | 4 | huwei | 杜蕾斯 | 12345 | | 6 | Lee | wawa | 123455 | | 3 | daoke | *mei | 1121212 | +-----+-----------+------------+---------+ select u.uid,u.username as ‘用户名‘,o.name as ‘商品名‘,o.buytime from users u,order_goods o where u.uid=o.uid; # 简化版 select users.uid as ID,users.username as ‘姓名‘,order_goods.name as "商品名",order_goods.buytime from users inner join order_goods on users.uid=order_goods.uid;
# 左连接 mysql> select users.uid as ID,users.username as ‘姓名‘,order_goods.name as "商品名",order_goods.buytime from users left join order_goods on users.uid=order_goods.uid; # 以左边的表为准,显示所有左边表中的记录,甚至右边表没有匹配的显示为NULL。 +----+----------+------------+---------+ | ID | 姓名 | 商品名 | buytime | +----+----------+------------+---------+ | 2 | zhicheng | iPhone11 | 1111112 | | 4 | huwei | 杜蕾斯 | 12345 | | 6 | Lee | wawa | 123455 | | 3 | daoke | *mei | 1121212 | | 1 | xuelin | NULL | NULL | | 5 | xinxin | NULL | NULL | +----+----------+------------+---------+ 6 rows in set (0.00 sec) # NULL 可以显示默认值 ifnull(表.字段,‘默认值‘) mysql> SELECT users.uid,users.username as 用户名, IFNULL(order_goods.name,"空空 如也") as 商品名 from users LEFT JOIN order_goods on users.uid=order_goods.uid;
# 右连接 mysql> select users.uid as ID,users.username as ‘姓名‘,order_goods.name as "商品名",order_goods.buytime from users right join order_goods on users.uid=order_goods.uid; # 以右边的表为准 +------+----------+------------+---------+ | ID | 姓名 | 商品名 | buytime | +------+----------+------------+---------+ | 2 | zhicheng | iPhone11 | 1111112 | | 4 | huwei | 杜蕾斯 | 12345 | | 6 | Lee | wawa | 123455 | | 3 | daoke | *mei | 1121212 | +------+----------+------------+---------+ 4 rows in set (0.00 sec)
# 子查询 SELECT * from users where uid in(2,3,4,6);# 查询购买用户的详细信息 括号中的值是写死的 SELECT * from users where uid in(select uid from order_goods);#先从一个表中查出记录再 将结果作为条件 从另外表中查询 # 用于子查询的关键字主要包括 in、not in、=、!=、exists、not exists 等。
# 记录联合 # 两个表的数据按照一定条件查询出来 结果并到一起显示 select uid from users union all select uid from order_goods; # 将结果集直接合并在一起 select uid from users union select uid from order_goods; # 去除重复记录