MySQL(二): 表的增删查改

文章目录

  • 新增 (Create)
    • 全列插入
    • 指定列插入
  • 查询 (Retrieve)
    • 全列查询
    • 指定列查询
    • 条件查询
    • 分页查询
    • 去重:DISTINCT
    • 排序:ORDER BY
    • 别名:AS
  • 更新 (Update)
  • 删除 (Delete)


CRUD增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写,是表的四种基本操作。

新增 (Create)

全列插入

语法

INSERT INTO [表名] VALUES(参数1, 参数2, 参数3......);

示例

INSERT INTO stu VALUES(2, 15, "李梅", 20200811, 88.5, 90.5);

MariaDB [lee]> select * from stu;
+------+------+--------+------------+-------+---------+
| id   | age  | name   | birth      | math  | english |
+------+------+--------+------------+-------+---------+
|    2 |   15 | 李梅   | 2020-08-11 | 88.50 |   90.50 |
+------+------+--------+------------+-------+---------+
1 row in set (0.000 sec)

指定列插入

即只插入部分列内容
语法

INSERT INTO [表名](1,列2,列3.......) values(参数1, 参数2, 参数3......);

示例

INSERT INTO stu(id, age, name) values(3, 16, "李华");

MariaDB [lee]> select * from stu;
+------+------+--------+-------+------+---------+
| id   | age  | name   | birth | math | english |
+------+------+--------+-------+------+---------+
|    3 |   16 | 李华   | NULL  | NULL |    NULL |
+------+------+--------+-------+------+---------+
1 row in set (0.001 sec)

查询 (Retrieve)

全列查询

语法

SELECT * FROM [表名];

示例

SELECT * FROM stu;

+------+------+--------+------------+-------+---------+
| id   | age  | name   | birth      | math  | english |
+------+------+--------+------------+-------+---------+
|    1 |   11 | 李华   | 2020-08-12 | 76.50 |   87.50 |
|    2 |   15 | 李梅   | 2020-08-13 | 88.50 |   90.50 |
|    3 |   18 | 张三   | 2020-08-14 | 84.50 |   70.50 |
|    4 |   16 | 李四   | 2020-08-15 | 75.60 |   40.80 |
|    5 |   14 | 王五   | 2020-08-16 | 48.80 |   91.90 |
|    6 |   13 | 马六   | 2020-08-17 | 85.90 |   95.40 |
+------+------+--------+------------+-------+---------+


指定列查询

语法

SELECT1,列2,列3..... FROM [表名];

示例

SELECT id, age, name FROM stu;

+------+------+--------+
| id   | age  | name   |
+------+------+--------+
|    1 |   11 | 李华   |
|    2 |   15 | 李梅   |
|    3 |   18 | 张三   |
|    4 |   16 | 李四   |
|    5 |   14 | 王五   |
|    6 |   13 | 马六   |
+------+------+--------+

条件查询

MySQL(二): 表的增删查改_第1张图片

语法

SELECT * FROM [表名] WHERE [条件];

示例

SELECT * FROM stu where name = "李华";

+------+------+--------+------------+-------+---------+
| id   | age  | name   | birth      | math  | english |
+------+------+--------+------------+-------+---------+
|    1 |   11 | 李华   | 2020-08-12 | 76.50 |   87.50 |
+------+------+--------+------------+-------+---------+

分页查询

语法

SELECT * FROM [表名] LIMIT [每页条数] OFFSET [偏移条数];

示例


SELECT * FROM stu LIMIT 2;//限制每页两条

+------+------+--------+------------+-------+---------+
| id   | age  | name   | birth      | math  | english |
+------+------+--------+------------+-------+---------+
|    1 |   11 | 李华   | 2020-08-12 | 76.50 |   87.50 |
|    2 |   15 | 李梅   | 2020-08-13 | 88.50 |   90.50 |
+------+------+--------+------------+-------+---------+

SELECT * FROM stu LIMIT 2 OFFSET 2;//偏移两条,也就是查询第二页

+------+------+--------+------------+-------+---------+
| id   | age  | name   | birth      | math  | english |
+------+------+--------+------------+-------+---------+
|    3 |   18 | 张三   | 2020-08-14 | 84.50 |   70.50 |
|    4 |   16 | 李四   | 2020-08-15 | 75.60 |   40.80 |
+------+------+--------+------------+-------+---------+


去重:DISTINCT

语法

SELECT DISTINCT * FROM [表名];

示例

SELECT DISTINCT * FROM stu;

+------+------+--------+------------+-------+---------+
| id   | age  | name   | birth      | math  | english |
+------+------+--------+------------+-------+---------+
|    1 |   11 | 李华   | 2020-08-12 | 76.50 |   87.50 |
|    2 |   15 | 李梅   | 2020-08-13 | 88.50 |   90.50 |
|    3 |   18 | 张三   | 2020-08-14 | 84.50 |   70.50 |
|    4 |   16 | 李四   | 2020-08-15 | 75.60 |   40.80 |
|    5 |   14 | 王五   | 2020-08-16 | 48.80 |   91.90 |
|    6 |   13 | 马六   | 2020-08-17 | 85.90 |   95.40 |
+------+------+--------+------------+-------+---------+

排序:ORDER BY

语法

SELECT * FROM [表名] ORDER BY [排序依赖项] [DESC/ASC];
//DESC为降序排序,ASC为升序排序,默认为ASC

示例

SELECT * FROM stu ORDER BY math DESC;//按数学成绩降序

+------+------+--------+------------+-------+---------+
| id   | age  | name   | birth      | math  | english |
+------+------+--------+------------+-------+---------+
|    2 |   15 | 李梅   | 2020-08-13 | 88.50 |   90.50 |
|    6 |   13 | 马六   | 2020-08-17 | 85.90 |   95.40 |
|    3 |   18 | 张三   | 2020-08-14 | 84.50 |   70.50 |
|    1 |   11 | 李华   | 2020-08-12 | 76.50 |   87.50 |
|    4 |   16 | 李四   | 2020-08-15 | 75.60 |   40.80 |
|    5 |   14 | 王五   | 2020-08-16 | 48.80 |   91.90 |
+------+------+--------+------------+-------+---------+

别名:AS

语法

SELECT 列a,列b...... AS [别名] FROM [表名];
//DESC为降序排序,ASC为升序排序,默认为ASC

示例

SELECT name, math + english AS total FROM stu;//获取每个人的总分

+--------+--------+
| name   | total  |
+--------+--------+
| 李华   | 164.00 |
| 李梅   | 179.00 |
| 张三   | 155.00 |
| 李四   | 116.40 |
| 王五   | 140.70 |
| 马六   | 181.30 |
+--------+--------+


更新 (Update)

语法

UPDATE [表名] SET [修改项] = [修改结果] 

示例

UPDATE stu SET math = 99.5 WHERE name = "李梅";

MariaDB [lee]> select * from stu;
+------+------+--------+------------+-------+---------+
| id   | age  | name   | birth      | math  | english |
+------+------+--------+------------+-------+---------+
|    2 |   15 | 李梅   | 2020-08-11 | 99.50 |   90.50 |
+------+------+--------+------------+-------+---------+

删除 (Delete)

语法

DELETE FROM [表名];

示例

DELETE FROM stu where name = "李梅";

MariaDB [lee]> select * from stu;
Empty set (0.000 sec)

你可能感兴趣的:(MySQL,mysql,sql,数据库,mariadb)