MySQL query statements are divided into 11 steps, each of which will produce a virtual table, the virtual table as a processing input, but these virtual tables are transparent to the user, but only the last virtual table will be returned as the result.
Statement execution Order:
Order |
Name |
Content |
1 |
SELECT |
Command initiation |
2 |
* or AVG (field), etc. |
Query content |
3 |
From |
Query location |
4 |
WHERE |
Conditional query |
5 |
GROUP by |
Group |
6 |
Having |
Screening |
7 |
ORDER by (ASC/DESC) |
Sort |
8 |
LIMIT |
Limit the number of results |
1. Single-table query
- Querying the specified column
-- Check the student's form for the number and name; Select from student;
-- Check all information in student tables from student;
- To alias a column after a query
-- Check the "name" and "Year of birth" columns for all students Select as name, as from student;
-- query position is a record of salesman;Select*fromwhere job=' Salesman'
--Query department records in 10 or 20Select * fromEmpwhereDeptno=Ten orDeptno= -;--Query Department in 10 or department at 20 and salary less than 2000 recordSelect * fromEmpwhereDeptno=Ten or(Deptno= - andSal<= -);
Go to re-query, use <DISTINCT column name > statement
-- Check the student number of the course Select distinct as from score;
-- query No course number and course name for the first course. select Cno as course number, Cname as course name, cpno from course where cpno is null ; -- Check all students with grades, course numbers and grades select Sno as , Cno as course number, Grade as score from SC where Grade is not null ;
- Convert NULL to actual value, using <COALESCE (column name, replace value) > statement
-- query allowance is null, replaced by 0 Select COALESCE (Comm,0 from EMP;
- The fuzzy query (like% _ statement)% denotes any character; _ Represents a single character
--query employee names starting with a recordSelect * fromEmpwhereEname like 'a%';--Query the employee name for a record that contains aSelect * fromEmpwhereEname like '%a%';--Query Employee Name The second letter begins with a recordSelect * fromEmpwhereEname like '_a%';
MySQL Query Statement Execution order