当前位置:网站首页>Mysql database, query
Mysql database, query
2022-07-24 02:30:00 【The end of the world and you】
Mysql database , Inquiry section
1. Sample table contents
dept surface :
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
emp surface :
+-------+--------+-----------+------+------------+---------+---------+--------+
| 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 surface :
+-------+-------+-------+
| GRADE | LOSAL | HISAL |
+-------+-------+-------+
| 1 | 700 | 1200 |
| 2 | 1201 | 1400 |
| 3 | 1401 | 2000 |
| 4 | 2001 | 3000 |
| 5 | 3001 | 9999 |
+-------+-------+-------+
2. Simple query
MySQL Database usage SQL SELECT Statement to query data .
For example, we want to query all the information of a table , You can do this :
Of course , This way of querying is inefficient , We recommend you to use multi column query :
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)
that , If you want to query the information of a single column ?
You can specify the column name to query :
mysql> select DNAME from dept;
+------------+
| DNAME |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
Query the information of multiple columns , It can be written like this :
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. Alias the list
During the query , We can also choose to list an alias :
mysql> select DNAME as NAME from dept;
+------------+
| NAME |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
Or you can skip it as keyword :
mysql> select DNAME NAME from dept;
+------------+
| NAME |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
In all databases , Single quotation marks are used for strings , It's a standard 🧨
4. Column operation
At query time , We can also directly perform column operations :
such as , We want to calculate the annual salary of our employees :
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. Conditions of the query
Example 1: The query salary is greater than or equal to 3000 Information about :
mysql> select empno,ename from emp where sal >= 3000;
+-------+-------+
| empno | ename |
+-------+-------+
| 7788 | SCOTT |
| 7839 | KING |
| 7902 | FORD |
+-------+-------+
3 rows in set (0.00 sec)
Example 2: Check salary at 2000 To 3000( contain 2000 and 3000) Information about :
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)
Example 3: Query employees whose subsidies are empty ( Not empty 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 Compare NULL Value cannot be used
=Number
Example 4: Query position is MANAGER And the salary is greater than or equal to 2500 Information about :
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)
Example 5: Query position is MANAGER perhaps SALESMAN The employees' :( Use keywords in)( Do not use between certain values 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)
Example 6: Fuzzy query , Find the letters in the name o Of :
mysql> select ename from emp where ename like '%o%';
+-------+
| ename |
+-------+
| JONES |
| SCOTT |
| FORD |
+-------+
3 rows in set (0.00 sec)
Example 7: Fuzzy query , Find the name to T At the end of the :
mysql> select ename from emp where ename like '%T';
+-------+
| ename |
+-------+
| SCOTT |
+-------+
1 row in set (0.00 sec)
Example 8: Fuzzy query , Find the name to K At the beginning :
mysql> select ename from emp where ename like 'K%';
+-------+
| ename |
+-------+
| KING |
+-------+
1 row in set (0.00 sec)
Example 9: Fuzzy query , Find out that the second letter of the name is A Of :
mysql> select ename from emp where ename like '_A%';
+--------+
| ename |
+--------+
| WARD |
| MARTIN |
| JAMES |
+--------+
3 rows in set (0.00 sec)
Example 10: Fuzzy query , Find out that the third letter of the name is A Of :
mysql> select ename from emp where ename like '__A%';
+-------+
| ename |
+-------+
| BLAKE |
| CLARK |
| ADAMS |
+-------+
3 rows in set (0.00 sec)
边栏推荐
- Login with a third-party account
- Mysql数据库,查询篇
- Sharing a case of controller restart caused by a CIFS bug in NetApp Fas series
- [diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu
- 我国科学家在高安全量子密钥分发网络方面取得新进展
- [diary of supplementary questions] [2022 Hangdian summer school 1] c-backpack
- After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words
- 营员招募|心怀世界的AI青年们,联合国需要你为可持续发展助力!
- Mysql数据库,排序与单行处理函数篇
- canvas-绘图(鼠标按下 绘制 抬起 结束)
猜你喜欢

营员招募|心怀世界的AI青年们,联合国需要你为可持续发展助力!

Cinq ans de contact avec près d'une centaine de patrons, en tant que chasseur de têtes, j'a i découvert que le secret de la promotion n'est que quatre mots

Research and analysis of the third-party dependency library Ag grid

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

Implementation of POP3 client code

Jina AI 联合Datawhale,发起学习项目!

"Why should we do IVX?"—— Interview with IVX CEO Meng Zhiping to understand IVX corporate culture

Network protocol details: TCP part1

College degree want to 0 basic programming after looking for a job feasible?

Use of component El scrollbar
随机推荐
This article shows you how to use SQL to process weekly report data
使用第三方账号登录
关于缺少编程基础的朋友想转行 ABAP 开发岗提出的一些咨询问题和解答
[datasets] - downloading some datasets of flyingthings3d optical flow
Maximize, minimize, restore, close and move the WinForm form form of C #
输入cnpm -v出现cnpm : 无法加载文件 C:\Users\19457\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
【补题日记】[2022牛客暑期多校1]J-Serval and Essay
Go basic notes_ 5_ Array slice
Installation, configuration and use of sentry
regular expression
[FPGA tutorial case 38] communication case 8 - serial parallel serial data transmission based on FPGA
[diary of supplementary questions] [2022 Niuke summer school 1] j-serval and essay
响应式布局一个网页在不同设备显示不同效果)meta:vp
C language curriculum - personal information management system (including student grades and consumption records)
What is naked SQL? What middleware or plug-in is good for express to operate MySQL?
Login with a third-party account
ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字
Rylstim Screen Recorder
Leetcode 70 climbing stairs, 199 right view of binary tree, 232 realizing queue with stack, 143 rearranging linked list