当前位置:网站首页>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 :

  1. Low efficiency , Because it will put * Convert to all fields .
  2. Poor readability .
  3. 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 :betweenandselect 
			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 nullselect 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 .

原网站

版权声明
本文为[Dutkig]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140336160423.html