当前位置:网站首页>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)
边栏推荐
- Wallys/DR4019S/IPQ4019/11ABGN/802.11AC/high power
- The difference between.Split (",", -1) and.Split (",")
- STM32概念和安装【第一天】
- async await详解 & Promise
- [diary of supplementary questions] [2022 Niuke summer school 1] j-serval and essay
- STM32 concept and installation [day 1]
- [重要通知]星球线上培训第三期来袭!讲解如何在QTYX上构建自己的量化策略!...
- Qml- use listview to build a three-level treeview architecture
- 程序员必备技能----断点调试(IDEA版)
- “我们为什么要做 iVX ? ” ——访 iVX CEO 孟智平 了解 iVX 企业文化
猜你喜欢

The communication principle between native components, applets and clients, and the operation principle of video, map, canvas, picker, etc

“我们为什么要做 iVX ? ” ——访 iVX CEO 孟智平 了解 iVX 企业文化

What's new in the ranking list in July? This language is invincible?

Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
![[jailhouse article] virtualization over multiprocessor system on chip an enabling paradigm for](/img/7b/95df873bfcad6b14c009d2681cf2f6.png)
[jailhouse article] virtualization over multiprocessor system on chip an enabling paradigm for

暗黑系王者,低照度图像增强技术解析

新红包封面平台可搭建分站独立后台的源码

关于缺少编程基础的朋友想转行 ABAP 开发岗提出的一些咨询问题和解答

Crud operation of mongodb (2)

Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
随机推荐
2022-07-22: what is the output of the following go language code? A:1; B:1.5; C: Compilation error; D:1.49。 package main import “fmt“ func main() { var i
Leetcode exercise -- two questions about the nearest common ancestor of binary trees
Canvas drawing (mouse click to draw and lift to end)
async await详解 & Promise
5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字
【补题日记】[2022牛客暑期多校1]I-Chiitoitsu
C language curriculum - personal information management system (including student grades and consumption records)
MySQL---four JDBC
Understand the transport layer protocol - tcp/udp
Redraw the button and make your own circular LED indicator
响应式pbootcms模板装修设计类网站
7 issues to consider before website construction
关于 SAP Fiori 应用的离线使用
Research and analysis of the third-party dependency library Ag grid
College degree want to 0 basic programming after looking for a job feasible?
Use the hiflow scene connector to view the epidemic situation in the region every day
View binding confusion. I have been studying confusion for two days.
Wallys/PD-60 802.3AT Input Output802.3AT/AT 85% Efficiency 10/100/1000M GE Surge Protection
POP3客户端代码的实现
【补题日记】[2022牛客暑期多校2]K-Link with Bracket Sequence I