MySQL database---addition, deletion, modification and query of MySQL table (basic)

Article directory

Adding, deleting, modifying and querying MySQL tables (basic)

1. CRUD

  • Comments: In SQL, you can use "–space + description" to indicate comments
  • CRUD is an acronym for the four words Add (Create), Query (Retrieve), Update (Update), and Delete (Delete).

2. Add (Create)

2.1 Full column insert

insert into [表名] values (对应的列的数据);

Note: The number and type of inserted fields need to be consistent with those required in the table structure

insert image description here

2.2 Specify column insertion

insert into [表名] (若干个指定列) values (对应的列的数据);

Insert only some of the columns as needed
insert image description here

2.3 Multi-line insert

insert into [表名] values (对应的列的数据) , (对应的列的数据)......;

insert image description here

3. Retrieve

3.1 Full column search

select * from [表名];

Note: * is a wildcard, which means to find all columns.

insert image description here

3.2 Specify column lookup

select [列名] from [表名];

insert image description here

3.3 The query field is an expression

3.3.1 Example: Find the total grades of all students

insert image description here

3.3.2 Example: Find the Chinese grades of all the students' grades, and add 10 points to the base

insert image description here

3.4 The query field specifies an alias.

select [列名] as [别名] from [表名];

Note: as can be omitted.
insert image description here

3.5 Deduplication

select distinc [若干个列名] from [表名];

insert image description here

3.6 Sorting

order by specifies which column to sort on
Note: The default is ascending order. ASC is ascending (small to large) DESC is descending (large to small)

  • NULL data is sorted, regarded as smaller than any value, ascending order appears at the top, descending order appears at the bottom
  • NULL plus all values ​​equal to NULL

3.6.1 Find information about classmates and sort them in ascending order of math scores

select * from [表名] order by [列名] (ascdesc);

insert image description here

3.6.2 Find information about classmates and sort them in descending order of math scores

insert image description here

3.6.3 Find student information and sort by total grades in descending order

select name,chinese + math + english from exam_result order by chinese + math + english desc;

insert image description here

3.6.4 Find student information and sort by total grade in descending order (using aliases)

select name,chinese + math + english as total from exam_result order by total desc;

insert image description here

3.6.5 Sort by Multiple Columns

First, sort all the student information in descending order of Chinese, then in descending order of mathematics, and then in descending order of English.

select * from exam_result order by chinese desc,math desc,english desc;

insert image description here

3.7 Conditional query (important)

comparison operator

operator illustrate
>, >=, <, <= greater than, greater than or equal to, less than, less than or equal to
= Equals, NULL is not safe, e.g. NULL = NULL results in NULL
<=> Equals, NULL safe, i.e. NULL <=> NULL results in TRUE(1)
!=, <> not equal to
BETWEEN a0 AND a1 Range match, [a0, a1], if a0 <= value <= a1, return TRUE(1)
IN (option, …) If it is any of the options, return TRUE(1)
IS NULL is NULL
IS NOT NULL not NULL
LIKE Fuzzy match. % means any number (including 0) of any character; _ means any one character

Logical Operators

operator illustrate
AND logical AND Multiple conditions must all be TRUE(1) for the result to be TRUE(1)
OR logical or Any one of the conditions is TRUE(1), the result is TRUE(1)
NOT logical inversion Condition is TRUE(1), result is FALSE(0)

3.7.1 Query the records whose Chinese is null in the query data.

Incorrect spelling null = null
insert image description here
Correct spelling:

select * from exam_result where chinese <=> null;
select * from exam_result where chinese is null;

insert image description here

3.7.2 Find information about students who have failed in English

select * from exam_result where english < 60;

insert image description here

3.7.3 Find information about students whose language is better than English

select * from exam_result where chinese > english ;

insert image description here

3.7.4 Find students whose total score is below 200

Aliases cannot be used in where
insert image description here

select name,chinese + math + english as total from exam_result where chinese + math + english > 200;

insert image description here

3.7.5 Find students with a Chinese score greater than 80 and an English score greater than 80

select * from exam_result where chinese > 80 and english > 80;

insert image description here

3.7.6 Find students with Chinese scores greater than 80 or English scores greater than 80

select * from exam_result where chinese > 80 or english > 80;

insert image description here
Note: AND has higher priority than OR. When using it at the same time, you need to use parentheses () to wrap the part that executes first

3.7.7 Find students whose Chinese scores are between [80, 90]

select * from exam_result where chinese between 80 and 90;

insert image description here

select * from exam_result where chinese >= 80 and chinese <= 90;

insert image description here

3.7.8 Find math scores of 45.0 or 60.0

select * from exam_result where math in (45.0,60.0);

insert image description here

select * from exam_result where math = 45.0 or math = 60.0;

insert image description here

3.7.9 Find the grades of all students surnamed Sun

select * from exam_result where name like '孙%';

insert image description here

select * from exam_result where name like '孙_';

insert image description here

3.7.10 Find all students whose Chinese scores begin with 9.

select * from exam_result where chinese like '9%';

insert image description here

3.8 Paging search

-- 从 0 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 从 s 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;

3.8.1 Find the top 3 students with the highest total scores in the student information.

select name,chinese + math + english as total from exam_result order by total desc limit 3;

insert image description here

3.8.2 Find the students with the highest total score 4 ~ 6

select name,chinese + math + english as total from exam_result order by total desc limit 3 offset 3;

insert image description here

3.8.3 If the number after limit is too large and exceeds the number of records, the returned result will not have any errors

select name,chinese + math + english as total from exam_result order by total desc limit 10 offset 3;

insert image description here

3.8.4 If the offset is too large, the result may be an empty

select name,chinese + math + english as total from exam_result order by total desc limit 3 offset 100;

insert image description here

4. Modify (Update)

update [表名] set [列名] = [修改的值], [列名] = [修改的值] where 子句;

4.1 Change Sun Wukong's math score to 80

update exam_result set math = 80 where name = '孙悟空';

insert image description here

4.2 Change Cao Mengde's math score to 50 and Chinese to 95.

update exam_result set math = 50,chinese = 95 where name = '曹孟德';

insert image description here

4.3 All students' Chinese scores are -10 points.

mysql> update exam_result set chinese = chinese - 10 ;

insert image description here

4.4 Add 10 points to the math scores of the students with the last three scores.

mysql> update exam_result set math = math + 10 order by chinese + math + english asc limit 3;

insert image description here

5. Delete

delete from [表名] where [筛选条件];

insert image description here

Guess you like

Origin blog.csdn.net/wwzzzzzzzzzzzzz/article/details/122823870