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

image-20220226111343636

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;
    

image-20220226114127546

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 
    

     Please add a picture description

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 )
notnot You can take non , Mainly used in is or in in
likelike 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'
  1. use >= and <=

    select empno,ename,sal from emp where sal >= 1600 and sal <= 3000;
    
  2. 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;
    

    image-20220226124253070

    because null A special , use = There is no result in comparison .

    image-20220226124238832

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
  1. 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

  2. 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');

image-20220226130209010

  • 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'
  1. The first way to write it
select * from emp where sal <> 1600 and sal <> 3000;
  1. The second way
select * from emp where not (sal = 1600 or sal = 3000);
  1. 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;

image-20220226130827647

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%';

image-20220226132139409

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 !

原网站

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