当前位置:网站首页>MySQL笔记5_操作数据
MySQL笔记5_操作数据
2022-04-23 06:07:00 【小叶很笨呐!】
MySQL中对于数据的相关操作
1. 插入完整数据
insert into <表名>(字段名1,字段名2...) values(值1,值2...)
mysql> insert into student(name,phone) values('张三','111111111');
Query OK, 1 row affected (0.00 sec)
2. 查询表中所有的数据
select * from 表名
mysql> select * from employee;
+-----+--------+----------+---------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+----------+---------------+-------------+
| 1 | 张三 | 10000 | 国防部 | 13888888888 |
| 2 | 王五 | 56454564 | android开发 | 845664468 |
| 3 | 李四 | 800 | 程序员 | 66666666666 |
| 4 | tom | 1111800 | 测试 | 44444444444 |
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 6 | cindy | 15550 | boss | 88888888888 |
| 7 | vscode | 5645464 | client | 845664468 |
+-----+--------+----------+---------------+-------------+
7 rows in set (0.00 sec)
3. 如果我们添加全部字段的数据,那么我们可以省略前面字段的名称
insert into <表名> values(值1,值2,...)
mysql> insert into student values(5,"李四","22222222");
Query OK, 1 row affected (0.00 sec)
4. 插入部分数据,字段和这个值对应即可
mysql> insert into student(name) values("cindy");
Query OK, 1 row affected (0.00 sec)
5. 修改数据
update <表名> set <字段>=<值> <条件where>
mysql> update employee set department="boss" where department="boos";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
6. 当where为空的时候,不能使用=null,而是需要使用is null
update student set age = 22 where phone is null;
7. 删除表中的数据
delete from <表名> <where条件>
8. 数据表中数据的详细查询
where条件:
| 符号 | 意义 |
|---|---|
| = | 等于 |
| <> | 不等于 |
| >= | 大于等于 |
| <= | 小于等于 |
| > | 大于 |
| < | 小于 |
| is null | 为空 |
| is not null | 不为空 |
| and | 并且 |
| or | 或者 |
| like | 类似 |
select * from <表名> [where 条件]
mysql> select * from employee where _id = 2;
+-----+--------+----------+---------------+-----------+
| _id | name | salary | department | phone |
+-----+--------+----------+---------------+-----------+
| 2 | 王五 | 56454564 | android开发 | 845664468 |
+-----+--------+----------+---------------+-----------+
1 row in set (0.00 sec)
mysql> select * from employee where salary is not null;
+-----+--------+----------+---------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+----------+---------------+-------------+
| 1 | 张三 | 10000 | 国防部 | 13888888888 |
| 2 | 王五 | 56454564 | android开发 | 845664468 |
| 3 | 李四 | 800 | 程序员 | 66666666666 |
| 4 | tom | 1111800 | 测试 | 44444444444 |
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 6 | cindy | 15550 | boss | 88888888888 |
| 7 | vscode | 5645464 | client | 845664468 |
+-----+--------+----------+---------------+-------------+
7 rows in set (0.00 sec)
mysql> select * from employee where _id > 4;
+-----+--------+---------+--------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+---------+--------------+-------------+
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 6 | cindy | 15550 | boss | 88888888888 |
| 7 | vscode | 5645464 | client | 845664468 |
+-----+--------+---------+--------------+-------------+
3 rows in set (0.00 sec)
- 查询_id在10~14之间的(两种方法 and 或 between and)
但是between and 前面的数值,一定要小于后面的数值
mysql> select * from employee where _id >= 3 and _id <= 6;
+-----+--------+---------+--------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+---------+--------------+-------------+
| 3 | 李四 | 800 | 程序员 | 66666666666 |
| 4 | tom | 1111800 | 测试 | 44444444444 |
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 6 | cindy | 15550 | boss | 88888888888 |
+-----+--------+---------+--------------+-------------+
4 rows in set (0.00 sec)
mysql> select * from employee where _id between 3 and 6;
+-----+--------+---------+--------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+---------+--------------+-------------+
| 3 | 李四 | 800 | 程序员 | 66666666666 |
| 4 | tom | 1111800 | 测试 | 44444444444 |
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 6 | cindy | 15550 | boss | 88888888888 |
+-----+--------+---------+--------------+-------------+
4 rows in set (0.00 sec)
- 把_id为1、5、7的挑选出来(where in 或 or)
mysql> select * from employee where _id in(1,5,7);
+-----+--------+---------+--------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+---------+--------------+-------------+
| 1 | 张三 | 10000 | 国防部 | 13888888888 |
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 7 | vscode | 5645464 | client | 845664468 |
+-----+--------+---------+--------------+-------------+
3 rows in set (0.00 sec)
mysql> select * from employee where _id = 1 or _id = 5 or _id =7;
+-----+--------+---------+--------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+---------+--------------+-------------+
| 1 | 张三 | 10000 | 国防部 | 13888888888 |
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 7 | vscode | 5645464 | client | 845664468 |
+-----+--------+---------+--------------+-------------+
3 rows in set (0.00 sec)
select 字段,字段... from <表名> [where 条件]
这个方法适用于有些字段是我们不需要看到的
mysql> select name,department from employee;
+--------+---------------+
| name | department |
+--------+---------------+
| 张三 | 国防部 |
| 王五 | android开发 |
| 李四 | 程序员 |
| tom | 测试 |
| jerry | 产品经理 |
| cindy | boss |
| vscode | client |
+--------+---------------+
7 rows in set (0.00 sec)
select 字段,字段... from <表名> order by <字段名> [<asc>或<desc>]
asc升序,desc降序
mysql> select * from employee order by salary asc;
+-----+--------+----------+---------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+----------+---------------+-------------+
| 3 | 李四 | 800 | 程序员 | 66666666666 |
| 1 | 张三 | 10000 | 国防部 | 13888888888 |
| 6 | cindy | 15550 | boss | 88888888888 |
| 4 | tom | 1111800 | 测试 | 44444444444 |
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 7 | vscode | 5645464 | client | 845664468 |
| 2 | 王五 | 56454564 | android开发 | 845664468 |
+-----+--------+----------+---------------+-------------+
7 rows in set (0.00 sec)
mysql> select * from employee order by department desc;
+-----+--------+----------+---------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+----------+---------------+-------------+
| 3 | 李四 | 800 | 程序员 | 66666666666 |
| 4 | tom | 1111800 | 测试 | 44444444444 |
| 1 | 张三 | 10000 | 国防部 | 13888888888 |
| 5 | jerry | 1111800 | 产品经理 | 22222222222 |
| 7 | vscode | 5645464 | client | 845664468 |
| 6 | cindy | 15550 | boss | 88888888888 |
| 2 | 王五 | 56454564 | android开发 | 845664468 |
+-----+--------+----------+---------------+-------------+
7 rows in set (0.00 sec)
select 字段 as 别名,字段 as 别名,... from <表名> where 条件
别名,别名的好处就是我们可以根据自己的喜好来显示字段
mysql> select name as "名字",department as "部门" from employee;
+--------+---------------+
| 名字 | 部门 |
+--------+---------------+
| 张三 | 国防部 |
| 王五 | android开发 |
| 李四 | 程序员 |
| tom | 测试 |
| jerry | 产品经理 |
| cindy | boss |
| vscode | client |
+--------+---------------+
7 rows in set (0.00 sec)
9. 模糊查询
select * from <表名> where <字段名> like <关键字%>
”%“为占位符
mysql> select * from employee where name like "张%";
+-----+--------+----------+------------+-------------+
| _id | name | salary | department | phone |
+-----+--------+----------+------------+-------------+
| 1 | 张三 | 10000 | 国防部 | 13888888888 |
| 9 | 张1 | 456456 | 路人 | 6456464 |
| 10 | 张2 | 45897 | 扫地僧 | 6556 |
| 11 | 张9 | 45564897 | 保安 | 12138 |
+-----+--------+----------+------------+-------------+
4 rows in set (0.00 sec)
版权声明
本文为[小叶很笨呐!]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_50957373/article/details/121028156
边栏推荐
- 解决:You have 18 unapplied migration(s). Your project may not work properly until you apply
- Relabel of Prometheus_ Configs and metric_ relabel_ Configs explanation and usage examples
- Prometheus alarm record persistence (historical alarm saving and Statistics)
- oracle对表字段的修改
- AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
- 双指针仪表盘读数(一)
- 一个DG环境的ORA-16047: DGID mismatch between destination setting and target database问题排查及监听VNCR特性
- Exception record-8
- [sm8150] [pixel4] LCD driver
- RAC环境集群组件gipc无法正确识别心跳网络状态问题分析
猜你喜欢

Encapsulate a set of project network request framework from 0

10g数据库使用大内存主机时不能启动的问题

Ali vector library Icon tutorial (online, download)

Winter combat camp hands-on combat - first understand the cloud foundation, hands-on practice ECS ECS ECS novice on the road to get the mouse cloud Xiaobao backpack shadowless

Dolphinscheduler配置Datax踩坑记录

Using Prom label proxy to implement label based multi tenant reading of Prometheus thanos

Itop4412 HDMI display (4.0.3_r1)

Dolphinscheduler集成Flink任务踩坑记录

Information:2021/9/29 10:01 - Build completed with 1 error and 0 warnings in 11s 30ms Error异常处理

Chaos帶你快速上手混沌工程
随机推荐
EMR Based offline data analysis - polite feedback
Abnormal record-11
iTOP4412 HDMI显示(4.0.3_r1)
ORACLE表有逻辑坏块时EXPDP导出报错排查
Oracle redo log产生量大的查找思路与案例
RAC环境报错ORA-00239: timeout waiting for control file enqueue排查
几款电纸书阅读器参数对比
通过rownum来进行区间查询
Abnormal record-13
When switch case, concatenate the error case and if of the conventional judgment expression and use L
Abnormal record-10
Itop4412 LCD backlight drive (PWM)
开篇:双指针仪表盘的识别
select命令产生redo日志问题的分析
Itop4412 HDMI display (4.0.3_r1)
組件化學習
Comparison between Prometheus thanos and cortex components
解决:You have 18 unapplied migration(s). Your project may not work properly until you apply
Itop4412 surfaceflinger (4.4.4_r1)
Dolphinscheduler调度spark任务踩坑记录