MySQL - select query and sorting

4. Simple query

Query a field: select field name from table name

Query two or more fields of the same table: select field name 1, field name 2, .... from table name

Query all fields in a table:

The first way:

select field name 1, field name 2,....... field name n from table name

The second way: (not recommended: low code efficiency, high running consumption)

select * from table name

Alias ​​the column (field): select field name (before change) as field name (after change) from table name {Although this method changes the name, the actual name of the field is still the original name, so you still need to change the name Based on the first name}, as can be omitted

Note: When there are spaces or Chinese in the alias, the alias should be added with single quotes and double quotes (double quotes can be used in mysql, but not in other databases, so it is not recommended)

Fields can use mathematical expressions:

When entering the command: select ename, sal from emp;

Then I want to know the total salary input in December: select ename, sal*12 from emp;

Of course, you can also alias it with *12: select ename,sal*12 as 'year_money' from emp;


5. Condition query

Equal to: = Not equal to: <> or !=

Window command: select field 1, field 2, ..... from table name where condition;

When entering the command: select ename,empno from emp where sal=800;

输入:select ename,empno,sal from emp where sal<>3000;

It is also possible to query strings:

Enter the command: select empno, sal from emp where ename='SMITH';

There are two ways to query the range that is greater than or equal to a certain number and less than or equal to a certain number:

The first type: >= and

例如:select ename,empno,sal from emp where sal>=1000 and sal

The second type: between..... and .... (Note: must follow the left small right large)

Query data equal to null in some cases:

Note: null in the database means nothing, not even 0, so it cannot be measured with an equal sign, instead of =null, use is null;

When not null in some cases:

is not null

and 和 or

and is the meaning of and, as follows:

select empno,ename,job,sal from emp where job ='manager' and sal > 2000;

or means or, as follows:

select empno,ename,job,sal from emp where job='manager' or job='salesman';

If and and or appear at the same time, there will be a priority problem, so we need to use parentheses to ensure the priority when we are not sure about the priority. (and has a higher priority than or)

例如:select ename,deptno,sal from emp where sal > 2000 and (deptno = 10 or deptno = 20);

in: equivalent to representing multiple or, when we need to query more, or is too cumbersome, so we use in to solve this problem.

Note: The data to be queried in in is not a range, but a specific number!

not in : Equivalent to inversion, we can query the data except the data in the brackets:

Enter the command: select ename,empno,sal from emp where sal not in(800,2450,3000);

like fuzzy query

% : represents multiple matches of any number of characters

_ : represents matching any character

For example

Query the name with R in the name: select ename from emp where ename like '%R%';

Query the name whose name starts with A: select ename from emp where ename like 'A%';

Query the name whose name ends with N: select ename from emp where ename like '%N';

Query the name whose second character is A: select ename from emp where ename like '_A%';

Query the name whose third character is R: select ename from emp where ename like '__R%';

If you search for names containing _, for example: '%_%', if you query directly like this, all names will be found, because _ represents any character, so if you want to query names with '_', you need to add a reverse slash \ escape character;

Written as: '%\_%'


6. Sorting

order by sort

select

.........

from

...........

where

...........

order by

............

Execution order: first execute the from query table, then where filter, then select to find out, and finally order by sort

1. Specify ascending order: asc (ascending order)

For example: select ename, sal from emp order by sal asc (where asc can be omitted)

2. Specify descending order: desc (descending order)

例如:select ename,sal from emp order by sal desc;

3. Multiple field sorting

例如:select ename,sal from emp order by sal asc, ename asc;

Note: The second field is determined according to the sorting result of the first field. If the first field has equal data, it will be sorted according to the second field, otherwise the second field will not be executed.

Understand: the query method can also be directly written as the number of columns in the query: (in the query field, the second field is sorted in ascending order)

select ename,sal from emp order by 2 asc;

Note: Although it is possible, it is not recommended, because in the later stage of project development, when the data in our database changes, checking the second column is not the second column at the beginning, and then there will be more troublesome bugs! ! !

In summary:

Case: The query salary is between 1200 and 2500, and the salary is required to be sorted in descending order. If the salary is the same, the name is required to be sorted in ascending order.

select ename,sal from emp where sal between 1200 and 2500 order by sal desc,ename asc;

Guess you like

Origin blog.csdn.net/m0_73968621/article/details/132467633