当前位置:网站首页>Review of MySQL (3): query operation
Review of MySQL (3): query operation
2022-06-12 18:18:00 【BKSW.】
MySq Review ( 3、 ... and ): Query operation
Grammar format :( Any one sql All statements are based on “;” At the end of the )
select Field name 1, Field name 2, Field name 3,…from Table name ;
Simple query statements
Query a field
Check the name of the employee
select ename frome emp;
select Is the key word ,select And field names are separated by spaces 1,from Represents the table to query , It is separated from the field by a space .

Query multiple fields
Query employee's number and name
select empno,enmae from emmp;Be careful : Query multiple fields ,select The fields in are separated by commas , The last field is in from The fields before cannot use commas .
Query all fields
You can put all the fields in select After statement , But if there are too many fields , inconvenient . You can use the following convenient ways to query all fields .
select * from Table name ;
select * from emp;
Calculate the employee's annual salary
List the employee's number , Name and annual salary
select empno,ename,sal*12 from emp;

select You can use operators in statements , But there are some problems , The field name of annual salary is not accurate . Use as Keyword rename the fields of the table .
Display the queried fields as user-defined names or Chinese
select empno ' Employee number ',ename as " Employee name ",sal as ' Annual salary ' from emp; -- Be careful : Although in cmd Windows can be without double or single quotation marks , But in the later standard format, the string must be added with English single quotation marks | Double quotes , among as You can omit it
Conditions of the query
Conditional query needs to use where sentence ,where Must be on from After the statement table .
The following operators are supported :
| Operator | explain |
|---|---|
| = | be equal to |
| <> or != | It's not equal to |
| < | Less than |
| <= | Less than or equal to |
| > | Greater than |
| >= | Greater than or equal to |
| between … and …. | Between two values , Equate to >= and <= |
| is null | by null(is not null Not empty ) |
| and | also |
| or | perhaps |
| in | contain , It's equivalent to more than one or(not in Not in this range ) |
| not | not You can take non , Mainly used in is or in in |
| like | like It is called fuzzy query , Support % Or underline match % Match any character underscore , An underscore matches only one character |
The equal sign operator
The inquiry salary is 500 The employees'
select empno,enmae,sal from empp where sal = 5000;Inquire about job by MANAGE The employees'
select empno,ename from emp where job = "manager"; -- because job For the string , So add double or single quotation marks select empno,ename from emp where job = 'manager'; -- mysql The statements of are case insensitive by default , So it can also be written as the following select empno,ename from emp where job = "MANAQER";
<> The operator
Inquiring about salary is not equal to 5000 The employees'
select empno,ename,sal from emp where sal <> 5000;It can also be written in the following way , But the first one
select empno,ename,sal from emp where sal != 5000;Querying a job position is not equal to MANAGER The employees'
select empno, ename from emp where job <> 'MANAGER'; -- Again , Double quotation marks or single quotation marks should be added to the string
between…and… The operator
- Check the salary in 1600 To 3000 The employees'
use >= and <=
select empno,ename,sal from emp where sal >= 1600 and sal <= 3000;use between…and…
select empno, ename, sal from emp where sal between 1600 and 3000;
is null Operator
null It's empty , But it's not an empty string , by null You can set this field to be left blank , If the query is null Field of , use is null
Query the employee whose allowance is empty
-- because null type 1 A special , You have to use is To compare select * from emp where comm is null;
because null A special , use = There is no result in comparison .

AND Operator
and The meaning of and , All conditions must be met
The job position is MANAGER, Salary greater than 2500 The employees'
select * from emp where job = "MANAGER" and sal > 2500;
OR Operator
or As long as the conditions are met , It is equivalent to containing
Query out job by manager perhaps job by salesman The employees'
select * from emp where job = 'MANAGER' or job = 'SALESMAN';
The priority of the expression
- Query salary greater than 1800, And the department code is 20 perhaps 30 People who
Wrong writing
select * from emp where sal > 1800 and deptno = 20 or deptno = 30;As a result, the salary will be less than 1800 The data of , The reason is the priority of the expression , Filter first sal > 1800 and deptno = 20, And then deptno = 30 The staff merged , So it's not right
Write it correctly
Try to use parentheses !
select * from emp where sal > 1800 and (deptno = 20 or deptno = 30);
IN Operator
in Means to include , It can be used or Express , however in It will be more concise .
- Find out job by manager perhaps job by salesman The employees'
select * from emp where job in ('manager','salesman');

- Find out the salary is 1600 perhaps 3000 The employees'
select * from emp where sal in (1600,3000);
NOT Operator
- Find out that the salary does not include 1600 And does not contain 3000 The employees'
- The first way to write it
select * from emp where sal <> 1600 and sal <> 3000;
- The second way
select * from emp where not (sal = 1600 or sal = 3000);
- The third way
select * from emp where sal not in (1600,3000);
- Find out that the allowance is not null All employees
select * from emp where sal is not null;

LIKE Operator
- like Fuzzy query can be realized ,like Support % Match the underline
- Check the name with M Employees at the beginning
select * from emp where ename like 'M%';
- Check the name with N All employees at the end
select * from emp where ename like '%N';
- The query name contains O All employees
select * from emp where enmae like '%O%';
- The second character in the query name is A All employees
select * from emp where ename like '_A%';

Like in % and _ The difference between ?
% Matches the number of occurrences of any character
The underscore can only match one character
Like Expressions in must be enclosed in single or double quotation marks !
边栏推荐
- 赛程更新| 2022微软与英特尔黑客松大赛火热报名中
- VirtualLab basic experiment tutorial -6 Blazed grating
- Use applet to quickly generate app in seven steps
- ES7 does not use parent-child and nested relationships to implement one to many functions
- Gossip about the 88 of redis source code
- 机器学习系列(5):朴素贝叶斯
- 01-复杂度
- Stream流注意点
- Leetcode151 flipping words in strings
- Gospel of audio and video developers, rapid integration of AI dubbing capability
猜你喜欢
![Interior design style type, rendering 100 invitation code [1a12]](/img/90/8bbfbe33c5b412498744c0ea0ed559.jpg)
Interior design style type, rendering 100 invitation code [1a12]

GD32F4xx控制DGUS 变量显示

USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate

Overall flow chart of kernel interrupt

Gd32f4xx controls dgus touch keys

MySQL learning notes

VirtualLab基础实验教程-4.单缝衍射

Introduction to service grid and istio - continued

Vant3+ts encapsulates uploader upload image component

General differences between SQL server versions released by Microsoft in different periods so far, for reference
随机推荐
Vant3+ts H5 pages are nested into apps to communicate with native apps
Self made calculator (1 realized by Boolean logic operation unit and control unit programming)
企业架构的第一性原理
Is it safe to open an account in flush
VirtualLab basic experiment tutorial -5 Poisson bright spot
C business serial number rule generation component
干货 | 一文搞定 pytest 自动化测试框架(二)
USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
GD32F4xx 与符合DLT645的电能表通信_2
ESP-IDF 添加自己的组件
Schéma de cristallisation différentielle active et différence entre LV - PECL, LVDS et hcsl
Leetcode491 increasing subsequence
Make good use of IDE, speed up R & D efficiency by 100%
Leetcode 718 longest common substring
同花顺能开户吗,同花顺在APP上可以直接开通券商安全吗
JS中的字符串(含leetcode例题)<持续更新~>
TypeScript类型声明文件(三)
JS quick sort
C#的变量
联想回应笔记本太多太杂乱:现在是产品调整期 未来将分为数字/Air/ Pro三大系列