当前位置:网站首页>MySQL (2) -- simple query, conditional query
MySQL (2) -- simple query, conditional query
2022-07-05 23:13:00 【Dutkig】
Simple query
The database file we used is still the one used in the previous section test.sql
SQL Statement is case insensitive , It can also be mixed , But be careful to use half width semicolon ;
ending , Let's start with a simple query :
- Query a field
select Field name from Table name ;
Be careful :select and from It's all keywords , Field names and table names are identifiers .
for example : Query department name
mysql> select dname from dept;
+------------+
| dname |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
- Query two or more fields
—— Separated by commas
for example : Query department number and department name
select deptno,dname from dept;
+--------+------------+
| deptno | dname |
+--------+------------+
| 10 | ACCOUNTING |
| 20 | RESEARCH |
| 30 | SALES |
| 40 | OPERATIONS |
+--------+------------+
- Query all fields
select * from dept;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
Disadvantages of this approach :
- Low efficiency , Because it will put
*
Convert to all fields . - Poor readability .
- In actual development, it is not recommended that , The actual development proposal indicates the required fields .
Alias the columns of the query ——as
keyword
mysql> select deptno,dname as deptname from dept;
+--------+------------+
| deptno | deptname |
+--------+------------+
| 10 | ACCOUNTING |
| 20 | RESEARCH |
| 30 | SALES |
| 40 | OPERATIONS |
+--------+------------+
Be careful : Just display the query result column name as deptname, The original table column name is still called :dname remember
:select Statement will never be modified .( Because it is only responsible for querying )
as Keywords can also be omitted
mysql> select deptno,dname deptname from dept;
Let's assume that when you use an alias , There are spaces in the alias , What do I do ?
mysql> select deptno,dname dept name from dept;
DBMS See such a statement , Conduct SQL Statement compilation , It's not grammatical , Compiler error . How to solve ?
select deptno,dname 'dept name' from dept; // Charizing Operator
select deptno,dname "dept name" from dept; // quotation marks
+--------+------------+
| deptno | dept name |
+--------+------------+
| 10 | ACCOUNTING |
| 20 | RESEARCH |
| 30 | SALES |
| 40 | OPERATIONS |
+--------+------------+
Be careful : In all databases , Strings are uniformly enclosed in single quotes
, Single quotation marks are standard , Double quotation marks in oracle Not in the database . But in mysql Can be used in . Again : All strings in the database are enclosed in single quotes . It's standard , Double quotation marks are not standard .
Columns are involved in mathematical operations
Calculate the annual salary of employees :sal*12
mysql> select ename,sal from emp;
+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| ALLEN | 1600.00 |
| WARD | 1250.00 |
| JONES | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| TURNER | 1500.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| FORD | 3000.00 |
| MILLER | 1300.00 |
+--------+---------+
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 |
+--------+----------+
Conclusion : Fields can use mathematical expressions
mysql> select ename,sal*12 as yearsal from emp; // names
+--------+----------+
| ename | yearsal |
+--------+----------+
| 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 |
+--------+----------+
mysql> select ename,sal*12 as ' Annual salary ' from emp; // The alias is Chinese , In single quotation marks .
+--------+----------+
| ename | Annual salary |
+--------+----------+
| 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 |
+--------+----------+
Conditions of the query
What is a conditional query ?—— Not all the data in the table are found out . It is found that the qualified .
Grammar format :
select
Field 1, Field 2, Field 3....
from
Table name
where
Conditions ;
Types of conditions :
① = be equal to
Query salary equals 800 Employee's name and number ?
select empno,ename from emp where sal = 800;
Inquire about SMITH Your number and salary ?
select empno,sal from emp where ename = 'SMITH'; // String using single quotes
② <> or != It's not equal to
Query salary is not equal to 800 Employee's name and number ?
select empno,ename from emp where sal != 800;
select empno,ename from emp where sal <> 800; // An unequal sign consisting of the less than sign and the greater than sign
③ Greater than or equal to
Query salary less than 2000 Employee's name and number ?
mysql> select empno,ename,sal from emp where sal < 2000;
+-------+--------+---------+
| empno | ename | sal |
+-------+--------+---------+
| 7369 | SMITH | 800.00 |
| 7499 | ALLEN | 1600.00 |
| 7521 | WARD | 1250.00 |
| 7654 | MARTIN | 1250.00 |
| 7844 | TURNER | 1500.00 |
| 7876 | ADAMS | 1100.00 |
| 7900 | JAMES | 950.00 |
| 7934 | MILLER | 1300.00 |
+-------+--------+---------+
Query salary less than or equal to 3000 Employee's name and number ?
select empno,ename,sal from emp where sal <= 3000;
Query salary greater than 3000 Employee's name and number ?
select empno,ename,sal from emp where sal > 3000;
Query salary greater than or equal to 3000 Employee's name and number ?
select empno,ename,sal from emp where sal >= 3000;
④between … and …. Between two values ( When using, it must be small on the left and large on the right , Otherwise, we can't find ) , Equate to >= and <=
Query salary in 2450 and 3000 Employee information between ? Include 2450 and 3000
The first way :>= and <= (and Is and means .)
select empno,ename,sal from emp where sal >= 2450 and sal <= 3000;
+-------+-------+---------+
| empno | ename | sal |
+-------+-------+---------+
| 7566 | JONES | 2975.00 |
| 7698 | BLAKE | 2850.00 |
| 7782 | CLARK | 2450.00 |
| 7788 | SCOTT | 3000.00 |
| 7902 | FORD | 3000.00 |
+-------+-------+---------+
The second way :between … and …
select
empno,ename,sal
from
emp
where
sal between 2450 and 3000;
Be careful
: between and It's a closed interval , Include values at both ends .
⑤ is null by null(is not null Not empty )
Query which employees' allowances / Subsidy is null?
mysql> select empno,ename,sal,comm from emp where comm = null;
Empty set (0.00 sec)
mysql> select empno,ename,sal,comm from emp where comm is null;
+-------+--------+---------+------+
| empno | ename | sal | comm |
+-------+--------+---------+------+
| 7369 | SMITH | 800.00 | NULL |
| 7566 | JONES | 2975.00 | NULL |
| 7698 | BLAKE | 2850.00 | NULL |
| 7782 | CLARK | 2450.00 | NULL |
| 7788 | SCOTT | 3000.00 | NULL |
| 7839 | KING | 5000.00 | NULL |
| 7876 | ADAMS | 1100.00 | NULL |
| 7900 | JAMES | 950.00 | NULL |
| 7902 | FORD | 3000.00 | NULL |
| 7934 | MILLER | 1300.00 | NULL |
+-------+--------+---------+------+
10 rows in set (0.00 sec)
Be careful
: In the database null You can't use the equal sign to measure . Need to use is null
, Because in the database null Means nothing , It's not a value , So you can't use the equal sign to measure .
Query which employees' allowances / The subsidy is not null?
select empno,ename,sal,comm from emp where comm is not null;
+-------+--------+---------+---------+
| empno | ename | sal | comm |
+-------+--------+---------+---------+
| 7499 | ALLEN | 1600.00 | 300.00 |
| 7521 | WARD | 1250.00 | 500.00 |
| 7654 | MARTIN | 1250.00 | 1400.00 |
| 7844 | TURNER | 1500.00 | 0.00 |
+-------+--------+---------+---------+
⑥and also
The job search is MANAGER And the salary is higher than 2500 Employee information ?
select
empno,ename,job,sal
from
emp
where
job = 'MANAGER' and sal > 2500;
+-------+-------+---------+---------+
| empno | ename | job | sal |
+-------+-------+---------+---------+
| 7566 | JONES | MANAGER | 2975.00 |
| 7698 | BLAKE | MANAGER | 2850.00 |
+-------+-------+---------+---------+
⑦or perhaps
The job search is MANAGER and SALESMAN The employees' ?
select empno,ename,job from emp where job = 'MANAGER';
select empno,ename,job from emp where job = 'SALESMAN';
select
empno,ename,job
from
emp
where
job = 'MANAGER' or job = '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 |
+-------+--------+----------+
and and or At the same time , Is there a priority problem ?
Query salary greater than 2500, And the department number is 10 or 20 Employees of the Department ?
select
*
from
emp
where
sal > 2500 and deptno = 10 or deptno = 20;
Analyze the problem of the above sentence ?—— and Priority ratio or high . The above statement will be executed first and, And then execute or.
So what does the above sentence mean ?
Find out that the salary is greater than 2500 And the department number is 10 The employees' , perhaps 20 Find out all the employees in the Department .
select
*
from
emp
where
sal > 2500 and (deptno = 10 or deptno = 20);
and and or At the same time ,and High priority . If you want to let or Execute first , Need to add “ parentheses ”, If you're not sure about priorities , Just add parentheses .
⑧in contain , It's equivalent to more than one or (not in Not in this range )
The job search is MANAGER and SALESMAN The employees' ?
select empno,ename,job from emp where job = 'MANAGER' or job = 'SALESMAN';
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 |
+-------+--------+----------+
Be careful
:in Not an interval .in Followed by the specific value .
Query salary is 800 and 5000 Employee information ?
select ename,sal from emp where sal = 800 or sal = 5000;
select ename,sal from emp where sal in(800, 5000); // This doesn't mean 800 To 5000 Find out .
+-------+---------+
| ename | sal |
+-------+---------+
| SMITH | 800.00 |
| KING | 5000.00 |
+-------+---------+
select ename,sal from emp where sal in(800, 5000, 3000);
// not in Represents data that is not among these values .
select ename,sal from emp where sal not in(800, 5000, 3000);
+--------+---------+
| ename | sal |
+--------+---------+
| ALLEN | 1600.00 |
| WARD | 1250.00 |
| JONES | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| TURNER | 1500.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| MILLER | 1300.00 |
+--------+---------+
not You can take non , Mainly used in is or in in
- is null
- is not null
- in
- not in
Next blog , We will enter fuzzy query , Current line processing function 、 Learning of grouping function .
边栏推荐
- 6-axis and 9-axis IMU attitude estimation
- 3D point cloud slam
- 3 find the greatest common divisor and the least common multiple
- Three.JS VR看房
- 终于搞懂什么是动态规划的
- LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
- SPSS analysis of employment problems of college graduates
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
- Marginal probability and conditional probability
- Error when LabVIEW opens Ni instance finder
猜你喜欢
利用LNMP实现wordpress站点搭建
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
2022.02.13 - SX10-30. Home raiding II
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Registration and skills of hoisting machinery command examination in 2022
Tensor attribute statistics
Vision Transformer (ViT)
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Thoroughly understand JVM class loading subsystem
随机推荐
TypeError: this. getOptions is not a function
Common JVM tools and optimization strategies
openresty ngx_ Lua request response
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
实现反向代理客户端IP透传
2.13 summary
并查集实践
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Debian 10 installation configuration
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Simple and beautiful method of PPT color matching
C Primer Plus Chapter 9 question 9 POW function
Hcip day 11 (BGP agreement)
Go language implementation principle -- lock implementation principle
What is the process of building a website
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
Use of shell:for loop
UVA – 11637 Garbage Remembering Exam (组合+可能性)
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Openresty ngx Lua regular expression