插入记录的基本语法

第一种语法

1
2
3
INSERT INTO 表 VALUES(值1, 值2, 值3);
-- 示例
INSERT INTO USER VALUES(2, 'homu', 'homuhomu');

用这种方法插入数据,表中有多少个字段就必须输入多少个值
不能多也不能少…
若有默认值,不想上传也可以输入 null

第二种语法

1
2
3
INSERT INTO 表(字段1, 字段2, 字段3, ...字段n) VALUES(值1, 值2, 值3, ...值n);
-- 示例
INSERT INTO users(id, username, password, age) VALUES(3, 'akemi', 'homuhomu', 12);

除非有必填字段需要写入值之外,如果有默认值的不想写可以不写,mysql 会自动补全默认值
相比第一种语法,这种更为常用

插入多条记录

1
INSERT INTO user(username, password) values('homu', 'homuhomu'), ('akemi', 'homuhomu');

查询记录

基础查询

1
2
3
select * from 表;
-- 示例
select * from users;
1
2
3
4
5
6
7
8
+----+----------+----------+------+-------------+
| id | username | password | age | mobile |
+----+----------+----------+------+-------------+
| 1 | akemi | homuhomu | 11 | 12312312312 |
| 2 | homu | homuhomu | 12 | 12312312312 |
| 3 | amu | password | NULL | NULL |
+----+----------+----------+------+-------------+
3 rows in set (0.00 sec)

指定字段查询

1
2
3
4
select 字段1, 字段2, from 表;
-- 示例
select username from users;
select username, password from users;

查询单个字段的不重复记录

1
2
3
select distinct 字段 from 表;
-- 示例
select distinct username from users;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql> select * from users;
+----+----------+----------+------+-------------+
| id | username | password | age | mobile |
+----+----------+----------+------+-------------+
| 1 | akemi | homuhomu | 11 | 17612312312 |
| 2 | homu | homuhomu | 12 | 17612312312 |
| 3 | amu | password | NULL | NULL |
| 4 | akemi | NULL | NULL | NULL |
+----+----------+----------+------+-------------+
mysql> select distinct username from users;
+----------+
| username |
+----------+
| akemi |
| homu |
| amu |
+----------+

使用 where 条件查询

1
2
3
4
select 字段 from 表 where 条件;
-- 示例
select * from users where age=12;
select * from users where id is null;

where 后面的条件

  • 比较运算符
1
2
3
4
5
6
> 大于
< 小于
>= 大于等于
<= 小于等于
!= 不等于
= 等于
  • 逻辑运算符
1
2
or  或
and 且
  • 示例
1
select * from users where id > 3 and age = 12;

排序查询到的结果

1
2
3
4
5
6
7
select 字段 from 表 order by 排序关键字;
-- 排序关键字
asc -- 升序 从小到大 默认
desc -- 降序 从大到小

-- 示例
select * from users where age is not null order by age desc;

多字段排序
在第一个字段两个值相同的情况下,对第二个字段进行排序

1
select 字段 from 表 order by 字段1 排序关键字 字段2 排序关键字 字段n 排序关键字;

结果集限制

对查询后的结果进行限制,只显示其一部分
第一条记录为 0

1
2
3
4
5
select 字段 from 表 limit 数量;
select 字段 from 表 limit 起点, 数量;
-- 示例
select * from users limit 0, 2;
-- 从第一条开始,查询两条数据

统计函数的使用

函数 作用
sum 求和
count 统计总数
max 最大值
min 最小值
avg 平均值
/ 其他 sql 函数
1
2
3
select 函数(字段) from 表;
-- 示例
select count(username) from users;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> select count(id) from users;
+-----------+
| count(id) |
+-----------+
| 4 |
+-----------+
1 row in set (0.00 sec)

mysql> select count(age) from users;
+------------+
| count(age) |
+------------+
| 2 |
+------------+
1 row in set (0.00 sec)
# null不会被计数

结果分组

将筛选出的字段根据某个字段分成组
字段 2 中相同的会被视为一组

1
select 字段 from 表 group by 字段2;

分组筛选

1
2
select 字段 from 表 group by 字段2 having 条件;
-- 只显示符合条件的分组

和 where 的区别:
having 用来筛选组,而 where 用来筛选记录

组合使用 sql

关键字 作用
select 选择的列
from 表
where 查询的条件
group by 根据 having 或字段分组
having 筛选分组
order by 排序属性
limit 限制数量

删除记录

条件删除

删除表记录的时候一定要加上 where 条件,否则会删除整张表
删除重要数据前一定要做备份

1
delete from 表 where 条件;

清空表记录

delete 和 truncate 是类似的
但是有一点不同
delete 返回被删除的记录的数量
truncate 返回 0

1
2
truncate table 表;
-- 清空表数据,并让自增id从1开始

更新记录

1
2
3
update 表 set 字段1=值1, 字段2=值2, 字段n=值n where 条件;
-- 示例
update users set username='amurita', password='yui' where username='a';

同时更新两张表

1
2
3
4
update 表1, 表2 set 字段1=值1, 字段2=值2, 字段n=值n where 条件;
-- 示例
update suki s, users u set s.mono='www', u.username='www' where s.id=u.id;
-- 将suki和users中 id相同的行的username和mono字段改为www