MySQL database - query

1. Single table data query

1) Query all fields in the entire table

select * from emp;

operation result: 

5eff1a8b3a394890a504879065959dd6.png

 2) Query some fields in a table

select empno, ename,sal,job  from emp;

operation result: 

0f0103d904c24f5db40a6d78999d260b.png

 3) Use arithmetic expressions when querying fields in the table

select empno, ename,sal,sal*1.08 from emp;
select empno, ename,sal, sal*12 from emp;
select empno, ename,sal, sal*12 +1000 from emp;

Note: In the Select statement, arithmetic operators can be used to create expressions for numeric data.

operation result:

d8bb26c8762d4c7ba3b291de891e0345.png

4) Use field aliases when querying

select empno as 员工编号, ename 员工姓名, sal*12 年薪  from emp;
select empno, ename "Ename", sal*12 "Anual Salary" from emp;
select sal*12+5000  as "年度工资(加年终奖)" from emp;

operation result:

eb236960f4424ff1b7d9e5868c546ddc.png

 Note:

If the alias contains spaces or other special characters or is case-sensitive, it needs to be enclosed in double quotes.

AS can be omitted

2. Query to remove duplicates

By default, the query results include all record rows that meet the conditions, including duplicate rows. 

like:

select deptno from emp;

operation result:

4eedd787270c46e1b0d01cc738dc7732.png

Use the DISTINCT keyword to remove duplicate rows from query results

like:

select distinct deptno from emp;

 operation result:

aa892c72943d47de8438f2cc0e235f37.png

 3. Sorting query

Use the order by clause to sort query results

Sorting methods include ascending order (asc, default) and descending order (desc):

select empno, ename, sal from emp order by sal;
select empno, ename, sal from emp order by sal desc ;

 operation result:

f207c23c22644de28ffe576589e226d5.png

 Sort by multiple fields:

select deptno, empno, ename, sal from emp order by deptno, sal;

 Sort using field aliases:

select empno, ename, sal*12 annsal from emp order by annsal;

 4. Paging query

1) Query the first three customer information

select * from emp limit 3

operation result:

6848953fb6744d448da83103ab8090f6.png

2) Query a total of 6 pieces of information counting from the third piece down

select * from emp limit 3,6

operation result:

2d4a4fb4a27a4e7097f6ce89c18fa2d3.png

 5. Merge query

Function: Combine (merge) multiple select statements into one select statement

Keywords:

1) union all: merge all regardless of whether there are duplicates or not.

2) Union: If there are duplicates, filter out the duplicates

3186d6edbccc4728816cea830a74450d.png

 6. where clause

select * from emp where deptno=10;
select * from emp where ename = 'JACK';
select * from emp where hiredate = '2020-12-12';

Notice:

Strings and date values ​​must be enclosed in single quotes                 

String case sensitivity

Date value format is sensitive, the default date format is 'YYYY-MM-DD HH:mm:ss'

 

 Not equal to: <>

select * from emp where deptno <> 30;

Between...and... 

select * from emp where sal between 1600 and 2900;

appear in a set: in

select * from emp where ename in('SMITH','CLARK','KING');

Fuzzy query: like

%: wildcard _: represents a character

select * from emp where ename like '_S%'; --第二个字符是S的

Non-null judgment: is null

select * from emp where comm is not null;

Operator precedence: Arithmetic > Concatenation > Comparison > Logical

select * from emp where job='SALESMAN' or job='CLERK' and sal>=1280;
select * from emp where (job='SALESMAN' or job='CLERK') and sal>=1280;
--使用小括号强行改变优先级

7. group by clause

Function: The GROUP BY clause divides the data in the table into several groups

select deptno, avg(sal) from emp group by deptno;
select  deptno, count(*),avg(sal) from emp group by deptno;

6534524c9adf4522b971c9601f152be8.png

 Precautions:

1. If a field that appears in the select list is not included in a multi-line function, then the field must also appear in the group by clause;

2. If there is no group by clause, the mixed use of fields (single-row functions) and multi-row functions is not allowed in the select list.

select empno, sal from emp; //合法
select avg(sal) from emp; //合法
select empno, avg(sal) from emp; //非法

 3. Multi-line functions are not allowed to be used in where clauses

select deptno, avg(sal) from emp
where avg(sal) > 2000;   //执行where时尚未执行groupby 及其他
group by deptno;

8. having clause

Function: Filter the results of grouped queries

Note 1: The having clause filters the grouped results. It can only appear after the group by clause, and the where clause must appear before the group by clause.

Note 2: where filters rows and having filters groups. having supports all where operators.

Example: List department IDs whose average salary is greater than 1,000

select deptno, avg(sal) from emp group by deptno having avg(sal) > 1000
  order by deptno;

ee993ab57434460e972e145e7bba5779.png

 

Guess you like

Origin blog.csdn.net/shengshanlaolin_/article/details/128455245