MySQL(2):表的增删查改

增加(insert)

全列插入

语法

insert into [表名] values(参数1, 参数2, 参数3 ......);

示例

insert into student values(3, "孙悟空", 22, "1998-2-28 15:08:11", 98, 96.5, 92.03);

MariaDB [study]> select * from student;
+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
+------+-----------+------+---------------------+---------+-------+---------+
1 row in set (0.00 sec)

指定列插入

语法

insert into [表名](1,2,3 ......) values(参数1, 参数2, 参数3 ......);

示例

insert into student(id, name, age, chinese, math, english) values(1, "猪八戒", 20, 60, 62.5, 58);

MariaDB [study]> select * from student;
+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
|    1 | 猪八戒    |   20 | NULL                |   60.00 | 62.50 |   58.00 |
+------+-----------+------+---------------------+---------+-------+---------+
2 rows in set (0.00 sec)

查询(select)

全列查询

语法

select * from [表名];

示例

select * from student;

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
|    1 | 猪八戒    |   20 | NULL                |   60.00 | 62.50 |   58.00 |
|    1 | 唐僧      |   16 | 2000-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
|    5 | 沙悟净    |   21 | 1999-05-08 21:10:18 |   72.00 | 75.08 |   70.03 |
|    6 | 白龙马    |   19 | 2000-03-26 14:10:58 |   70.00 | 76.08 |   71.04 |
+------+-----------+------+---------------------+---------+-------+---------+
5 rows in set (0.00 sec)

指定列查询

语法

select 列1,2,3 ...... from [表名];

示例

select id, name, age from student;

+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    3 | 孙悟空    |   22 |
|    1 | 猪八戒    |   20 |
|    1 | 唐僧      |   16 |
|    5 | 沙悟净    |   21 |
|    6 | 白龙马    |   19 |
+------+-----------+------+
5 rows in set (0.00 sec)

排列查询

语法

select * from [表名] order by [依据的列] [asc/desc];

示例
默认升序排列(asc)

select * from student order by id;

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    1 | 猪八戒    |   20 | NULL                |   60.00 | 62.50 |   58.00 |
|    1 | 唐僧      |   16 | 2000-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
|    5 | 沙悟净    |   21 | 1999-05-08 21:10:18 |   72.00 | 75.08 |   70.03 |
|    6 | 白龙马    |   19 | 2000-03-26 14:10:58 |   70.00 | 76.08 |   71.04 |
+------+-----------+------+---------------------+---------+-------+---------+
5 rows in set (0.00 sec)

降序排列(desc)

select * from student order by id desc;

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    6 | 白龙马    |   19 | 2000-03-26 14:10:58 |   70.00 | 76.08 |   71.04 |
|    5 | 沙悟净    |   21 | 1999-05-08 21:10:18 |   72.00 | 75.08 |   70.03 |
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
|    1 | 猪八戒    |   20 | NULL                |   60.00 | 62.50 |   58.00 |
|    1 | 唐僧      |   16 | 2000-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
+------+-----------+------+---------------------+---------+-------+---------+
5 rows in set (0.00 sec)

分页查询

语法
在所有数据中查询前n条

select * from [表名] limit n;

在所有数据中从第s条开始查询前n条

select * from [表名] limit n offset s;

示例

select * from student limit 3;

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
|    1 | 猪八戒    |   20 | NULL                |   60.00 | 62.50 |   58.00 |
|    1 | 唐僧      |   16 | 2000-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
+------+-----------+------+---------------------+---------+-------+---------+
3 rows in set (0.00 sec)
select * from student limit 2 offset 2;

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    1 | 唐僧      |   16 | 2000-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
|    5 | 沙悟净    |   21 | 1999-05-08 21:10:18 |   72.00 | 75.08 |   70.03 |
+------+-----------+------+---------------------+---------+-------+---------+
2 rows in set (0.00 sec)

多字段的表达式查询

语法

select 列1 +2 ...... from [表名];

示例

select id, name, chinese + math from student;

+------+-----------+----------------+
| id   | name      | chinese + math |
+------+-----------+----------------+
|    3 | 孙悟空    |         194.50 |
|    1 | 猪八戒    |         122.50 |
|    1 | 唐僧      |         162.56 |
|    5 | 沙悟净    |         147.08 |
|    6 | 白龙马    |         146.08 |
+------+-----------+----------------+
5 rows in set (0.00 sec)

字段的别名查询

语法

select 列1 +2 as 别名 from [表名];

示例

select id, name, chinese + math + english as score from student;

+------+-----------+--------+
| id   | name      | score  |
+------+-----------+--------+
|    3 | 孙悟空    | 286.53 |
|    1 | 猪八戒    | 180.50 |
|    1 | 唐僧      | 248.56 |
|    5 | 沙悟净    | 217.11 |
|    6 | 白龙马    | 217.12 |
+------+-----------+--------+
5 rows in set (0.00 sec)

数据的去重查询

语法
根据某一字段,进行数据去重后,再查询

select distinct 列 ...... from [表名];

示例

select distinct id from student;

+------+
| id   |
+------+
|    3 |
|    1 |
|    5 |
|    6 |
+------+
4 rows in set (0.00 sec)

条件查询

比较运算符

操作符描述
=, <=>检测两个值是否相等
!=, <>检测两个值是否不相等
>检测左边的值是否大于右边的值
<检测左边的值是否小于右边的值
>=检测左边的值是否大于等于右边的值
<=检测左边的值是否小于等于右边的值
like模糊匹配
in判断查询的数据是否符合给与的多个选项数据之一
between…and…判断查询范围是否介于两个值之间

逻辑运算符

操作符描述
and逻辑与,两个条件都满足返回true
or逻辑或,两个条件任意满足一个返回true
not逻辑非,若不满足条件返回true

语法

select * from [表名] where [条件];

示例
=的使用

select * from student where name = "孙悟空";

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
+------+-----------+------+---------------------+---------+-------+---------+
1 row in set (0.00 sec)

in的使用

select * from student where name in ("唐僧", "沙悟净");

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    1 | 唐僧      |   16 | 2000-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
|    5 | 沙悟净    |   21 | 1999-05-08 21:10:18 |   72.00 | 75.08 |   70.03 |
+------+-----------+------+---------------------+---------+-------+---------+
2 rows in set (0.00 sec)

between…and…的使用

select * from student where chinese between 80 and 100;

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
|    1 | 唐僧      |   16 | 2000-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
+------+-----------+------+---------------------+---------+-------+---------+
2 rows in set (0.00 sec)

like的使用

select * from student where name like "白%" or math > 80;

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
|    1 | 唐僧      |   16 | 2000-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
|    6 | 白龙马    |   19 | 2000-03-26 14:10:58 |   70.00 | 76.08 |   71.04 |
+------+-----------+------+---------------------+---------+-------+---------+
3 rows in set (0.00 sec)

修改数据(update)

注意不能把状态条件忘记,否则修改是针对整张表的
语法

update [表名] set 列1 = val1 ...... where [条件];

示例

update student set birth = "2004-10-1 07:21:03" where name = "唐僧";

+------+--------+------+---------------------+---------+-------+---------+
| id   | name   | age  | birth               | chinese | math  | english |
+------+--------+------+---------------------+---------+-------+---------+
|    1 | 唐僧   |   16 | 2004-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
+------+--------+------+---------------------+---------+-------+---------+
1 row in set (0.00 sec)

删除数据(delete)

注意不能把状态条件忘记,否则删除是针对整张表的
语法

delete from [表名] where [条件];

示例

delete from student where name = "白龙马";

+------+-----------+------+---------------------+---------+-------+---------+
| id   | name      | age  | birth               | chinese | math  | english |
+------+-----------+------+---------------------+---------+-------+---------+
|    3 | 孙悟空    |   22 | 1998-02-28 15:08:11 |   98.00 | 96.50 |   92.03 |
|    1 | 猪八戒    |   20 | NULL                |   60.00 | 62.50 |   58.00 |
|    1 | 唐僧      |   16 | 2004-10-01 07:21:03 |   80.06 | 82.50 |   86.00 |
|    5 | 沙悟净    |   21 | 1999-05-08 21:10:18 |   72.00 | 75.08 |   70.03 |
+------+-----------+------+---------------------+---------+-------+---------+
4 rows in set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值