当前位置:网站首页>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 .
边栏推荐
- Getting started stm32--gpio (running lantern) (nanny level)
- 查看网页最后修改时间方法以及原理简介
- Selenium+pytest automated test framework practice
- Composition of interface
- Hcip day 12 (BGP black hole, anti ring, configuration)
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
- 透彻理解JVM类加载子系统
- Multi view 3D reconstruction
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
猜你喜欢
TypeError: this. getOptions is not a function
Non rigid / flexible point cloud ICP registration
From the perspective of quantitative genetics, why do you get the bride price when you get married
Practice of concurrent search
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
One article deals with the microstructure and instructions of class
一文搞定JVM常见工具和优化策略
Go语言实现原理——锁实现原理
Yiwen gets rid of the garbage collector
Ultrasonic sensor flash | LEGO eV3 Teaching
随机推荐
leecode-学习笔记
判断二叉树是否为完全二叉树
Common JVM tools and optimization strategies
2:第一章:认识JVM规范1:JVM简介;
UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
Multi camera stereo calibration
Initial experience | purchase and activate typora software
Leetcode sword finger offer brush questions - day 21
东南亚电商指南,卖家如何布局东南亚市场?
Openresty ngx Lua regular expression
Common model making instructions
Selenium+Pytest自动化测试框架实战
Tensor attribute statistics
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
【Note17】PECI(Platform Environment Control Interface)
Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
Marginal probability and conditional probability
Registration and skills of hoisting machinery command examination in 2022