Summary of the most commonly used SQL commands
Let's take the student table as an example:
Basic query
- Table full information:
select * from 表名称; //*可以理解代表全部
eg:
We query the entire student table:
select * from student;
The results are as follows:
- Search query
SELECT 列名称 FROM 表名称
eg:
We retrieve the student ID and name of all students:
Select Sno,Sname from student;
The results are as follows:
Change the display of column headings during query (take alias)
Select column1 as column1`,column2 as column2` from student;
eg:
We retrieve the student ID, name, and gender information of all students, and add alias information of "student ID", "name", and "sex" respectively:
Select Sno as 学号,Sname as 姓名,Ssex as 性别 from student;
The results are as follows:
Condition query
SELECT 列名称 FROM 表名称 WHERE 列 运算符 值;
eg1:
We inquire about the student ID, course ID, and result of students whose scores are greater than 90 points:
Select * from Student where Grade>90;
The results are as follows:
Operation operator:
eg2:
We check the student ID, course ID, and grades of students whose scores are between 85 and 90 points:
Select * from Student where Grade between 85 and 90;
We check the student ID, course ID, and grades of students whose scores are not between 90 and 95 points:
Select * from Student where Grade not between 90 and 95;
The results are as follows:
eg3:
We query the student number of the student whose elective course number is "2" and whose grade is greater than 88:
Select * from Student where Cno = '2' and Grade > 88;
The results are as follows:
Select * from Student where Cno = '2' or Cno = '3';
The results are as follows:
Data query based on IN clause
SELECT 列名
FROM 表名
WHERE 列名 IN (value1,value2,...)
eg:
We query all the information of Liu Chen and Li Yong from the student table:
Select * from Student where Sname in ('李勇','刘晨');
The results are as follows:
Query based on Like clause
- Use% wildcard
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
eg:
We retrieved the data of all the classmates surnamed Zhang from the student table:
Select * from Student where Sname like '刘%'; //%代表缺少的东西,可定义通配符
The results are as follows:
- Use _ wildcard
We retrieve the data of all students whose name is "勇" in the second word:
Select * from Student where Sname like '_勇%';
The results are as follows:
eg:
We retrieve the data of all students whose names contain the word "勇":
Select * from Student where Sname like '%勇%';
The results are as follows:
Special case
eg:
If the matching string of the user's query contains% or _, such as scribbled names, such as: Xie_ying, Li%feng, we need to query the student ID of the above classmate by name:
用 escape
Select Sno from Student where Sname like '谢/_颖' escape'/';
Select Sno from Student where Sname like '李/%峰' escape'/';
- Use [charlist] wildcard
eg:
We hope to select all the information of foreigners whose names begin with "A" and "L":
Select * from Student where Sname like '[AL]%';
We do not want to select all the information of foreigners whose names begin with "A" and "L":
Select * from Student where Sname like '[!AL]%';
Use top keyword query
SELECT TOP number|percent column_name(s)
FROM table_name;
eg:
We retrieve the first 3 student information from the Student table:
Select top 3 * from Student;
We retrieve the top 50% student information from the Student table:
Select top 50 percent * from Student;
Eliminate duplicate rows
Use distinct
eg:
Select distinct Sno from Student;
The results are as follows: