当前位置:网站首页>Mysql数据库,查询篇
Mysql数据库,查询篇
2022-07-24 02:25:00 【世界尽头与你】
1.示例表内容
dept表:
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
emp表:
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
salgrade表:
+-------+-------+-------+
| GRADE | LOSAL | HISAL |
+-------+-------+-------+
| 1 | 700 | 1200 |
| 2 | 1201 | 1400 |
| 3 | 1401 | 2000 |
| 4 | 2001 | 3000 |
| 5 | 3001 | 9999 |
+-------+-------+-------+
2.简单查询
MySQL 数据库使用SQL SELECT语句来查询数据。
例如我们要查询一个表的全部信息,可以这样做:
当然,这种方式进行查询的效率较低,我们更推荐您使用多列查询的方式:
mysql> select * from dept;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
4 rows in set (0.00 sec)
那么,如果想查询单个列的信息呢?
可以指定列名进行查询:
mysql> select DNAME from dept;
+------------+
| DNAME |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
查询多个列的信息,可以这样写:
mysql> select DNAME , LOC from dept;
+------------+----------+
| DNAME | LOC |
+------------+----------+
| ACCOUNTING | NEW YORK |
| RESEARCH | DALLAS |
| SALES | CHICAGO |
| OPERATIONS | BOSTON |
+------------+----------+
4 rows in set (0.00 sec)
3.给列起别名
在查询的过程中,我们还可以选择给列起一个别名:
mysql> select DNAME as NAME from dept;
+------------+
| NAME |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
也可以不加 as 关键字:
mysql> select DNAME NAME from dept;
+------------+
| NAME |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
在所有的数据库中,字符串统一使用单引号,这是一个标准🧨
4.列运算
在查询的时候,我们也可以直接进行列运算操作:
比如,我们想计算员工的年薪:
mysql> select ename,sal*12 from emp;
+--------+----------+
| ename | sal*12 |
+--------+----------+
| SMITH | 9600.00 |
| ALLEN | 19200.00 |
| WARD | 15000.00 |
| JONES | 35700.00 |
| MARTIN | 15000.00 |
| BLAKE | 34200.00 |
| CLARK | 29400.00 |
| SCOTT | 36000.00 |
| KING | 60000.00 |
| TURNER | 18000.00 |
| ADAMS | 13200.00 |
| JAMES | 11400.00 |
| FORD | 36000.00 |
| MILLER | 15600.00 |
+--------+----------+
14 rows in set (0.00 sec)
5.条件查询
示例1:查询工资大于等于3000的信息:
mysql> select empno,ename from emp where sal >= 3000;
+-------+-------+
| empno | ename |
+-------+-------+
| 7788 | SCOTT |
| 7839 | KING |
| 7902 | FORD |
+-------+-------+
3 rows in set (0.00 sec)
示例2:查询工资在2000到3000(包含2000和3000)的信息:
mysql> select empno,ename from emp where sal between 2000 and 3000;
+-------+-------+
| empno | ename |
+-------+-------+
| 7566 | JONES |
| 7698 | BLAKE |
| 7782 | CLARK |
| 7788 | SCOTT |
| 7902 | FORD |
+-------+-------+
5 rows in set (0.00 sec)
示例3:查询员工补助为空的(不为空为is not null):
mysql> select empno,ename from emp where comm is null;
+-------+--------+
| empno | ename |
+-------+--------+
| 7369 | SMITH |
| 7566 | JONES |
| 7698 | BLAKE |
| 7782 | CLARK |
| 7788 | SCOTT |
| 7839 | KING |
| 7876 | ADAMS |
| 7900 | JAMES |
| 7902 | FORD |
| 7934 | MILLER |
+-------+--------+
10 rows in set (0.00 sec)
Mysql比较NULL值不能使用
=号
示例4:查询岗位为MANAGER并且工资大于等于2500的信息:
mysql> select * from emp where JOB = "MANAGER" and SAL >= 2500;
+-------+-------+---------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+-------+---------+------+------------+---------+------+--------+
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
+-------+-------+---------+------+------------+---------+------+--------+
2 rows in set (0.00 sec)
示例5:查询岗位为MANAGER或者SALESMAN的员工:(使用关键字in)(不在某几个值之间使用not in)
mysql> select empno,ename,job from emp where job in ('MANAGER','SALESMAN');
+-------+--------+----------+
| empno | ename | job |
+-------+--------+----------+
| 7499 | ALLEN | SALESMAN |
| 7521 | WARD | SALESMAN |
| 7566 | JONES | MANAGER |
| 7654 | MARTIN | SALESMAN |
| 7698 | BLAKE | MANAGER |
| 7782 | CLARK | MANAGER |
| 7844 | TURNER | SALESMAN |
+-------+--------+----------+
7 rows in set (0.00 sec)
示例6:模糊查询,找出名字中含有字母o的:
mysql> select ename from emp where ename like '%o%';
+-------+
| ename |
+-------+
| JONES |
| SCOTT |
| FORD |
+-------+
3 rows in set (0.00 sec)
示例7:模糊查询,找出名字以T结尾的:
mysql> select ename from emp where ename like '%T';
+-------+
| ename |
+-------+
| SCOTT |
+-------+
1 row in set (0.00 sec)
示例8:模糊查询,找出名字以K开头的:
mysql> select ename from emp where ename like 'K%';
+-------+
| ename |
+-------+
| KING |
+-------+
1 row in set (0.00 sec)
示例9:模糊查询,找出名字第二个字母是A的:
mysql> select ename from emp where ename like '_A%';
+--------+
| ename |
+--------+
| WARD |
| MARTIN |
| JAMES |
+--------+
3 rows in set (0.00 sec)
示例10:模糊查询,找出名字第三个字母是A的:
mysql> select ename from emp where ename like '__A%';
+-------+
| ename |
+-------+
| BLAKE |
| CLARK |
| ADAMS |
+-------+
3 rows in set (0.00 sec)
边栏推荐
- [knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import
- Wenxin big model raises a new "sail", and the tide of industrial application has arrived
- Leetcode 203. remove linked list elements (2022.07.22)
- “我们为什么要做 iVX ? ” ——访 iVX CEO 孟智平 了解 iVX 企业文化
- Hydrogen entrepreneurship competition | Liu Xiaoqi, chairman of Guohua Investment: take advantage of the integration of scenery, hydrogen storage and finance to host the entrepreneurship competition a
- Use of component El scrollbar
- View binding confusion. I have been studying confusion for two days.
- 餐饮连锁门店重塑增长背后的数字化转型
- [jailhouse article] virtualization over multiprocessor system on chip an enabling paradigm for
- Visual full link log tracking
猜你喜欢

MySQL---four JDBC

The difference between.Split (",", -1) and.Split (",")
In depth understanding of the underlying framework of wechat applet (II) component system, exprser

One year after graduation, I gave up the internship opportunity and taught myself software testing at home. The internship of my classmates has just ended. I have become a 12K monthly salary testing e

Leetcode 70 climbing stairs, 199 right view of binary tree, 232 realizing queue with stack, 143 rearranging linked list
![[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import](/img/4b/c24ac8a11d15285a49d7b3b9bde4e3.png)
[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import
![[diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu](/img/be/47b8a86399f760e7cd6181528884c6.png)
[diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu

Jina AI 联合Datawhale,发起学习项目!
![【补题日记】[2022牛客暑期多校1]C-Grab the Seat](/img/86/1cf3bbc53d9365bb95dae6d532e276.png)
【补题日记】[2022牛客暑期多校1]C-Grab the Seat

ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
随机推荐
[jailhouse article] virtualization over multiprocessor system on chip an enabling paradigm for
Qt开发串口通讯软件中的数据转换问题:读取时_QByteArray转string;发送时_数据格式;int转16进制格式string;string中截取字符;16进制数加法;string转ByteAr
[untitled]
【MySQL】字符集utf8mb4无法存储表情踩坑记录
因果学习开源项目:从预测到决策!
【FPGA教程案例39】通信案例9——基于FPGA的交织-解交织数据传输
使用第三方账号登录
Jina AI 联合Datawhale,发起学习项目!
What is naked SQL? What middleware or plug-in is good for express to operate MySQL?
[C language] preprocessing details
MySQL---four JDBC
Graduation design campus information publishing platform website source code
【FPGA教程案例38】通信案例8——基于FPGA的串并-并串数据传输
Crud operation of mongodb (2)
BPG笔记(三)
Is software testing still popular in 2022?
【补题日记】[2022牛客暑期多校1]J-Serval and Essay
Pbootcms template calls the tag ordinal number from 2 or automatic number
[important notice] the third phase of planet online training is coming! Explain how to build your own quantitative strategy on qtyx
关于缺少编程基础的朋友想转行 ABAP 开发岗提出的一些咨询问题和解答