1. Createinsert into [table name] (field1, field2,....) value (value1, value2, ...); insert into [table name] (field 1, field 2, ....) values (value1, ...), (value2, ...), (value3, ...); Example: Create a student grades table CREATE TABLE exam_result ( id INT, name VARCHAR(20), chinese DECIMAL(3,1), math DECIMAL(3,1), english DECIMAL(3,1) ); 1.1 Single row data + full column insert -- Insert two records. The number of value_list must be consistent with the number and order of columns in the definition table. insert into exam_result value (1, 'tom', 68, 98, 56); insert into exam_result value ( 2, 'jum', 87.5, 78, 77); Each time data is inserted, it is a record containing several columns~~ The number and data type of the columns should correspond to the structure of the table. If the specified column is omitted before value, the entire column is inserted by default. 1.2 Multiple rows of data + specified columns - Insert two records. The number of value_list must be consistent with the number and order of the specified columns. INSERT INTO exam_result (id,name, chinese, math, english) VALUES (1,'tom', 67, 98, 56), (2,'jum', 87.5, 78, 77), (3,'lim', 88, 98.5, 90), (4,'tim', 82, 84, 67), (5,'huy', 55.5, 85, 45), (6,'sun', 70, 73, 78.5), (7,'ming', 75, 65, 30); 2. Retrieve2.1 Full column query select * from [table name]; * represents a wildcard, which means to search all columns 2.2 Query by specified column select [specify query column] from [table name]; 2.3 Query fields as expressions select [field expression] from [table name]; 2.4 Aliases select colum [as] [column name] from [table name]; 2.5 Deduplication: DISTINCT Use the distinct keyword to remove duplicate data from a column --98 points repeated select math from exam_result; +------+ |math| +------+ | 98.0 | | 78.0 | | 98.0 | | 84.0 | | 85.0 | | 73.0 | | 65.0 | +------+ -- Remove duplicate results select distinct math from exam_result; +------+ |math| +------+ | 98.0 | | 78.0 | | 84.0 | | 85.0 | | 73.0 | | 65.0 | +------+ 2.6 Sorting: ORDER BY select * from [table name] order by [sort field]; Use order by to specify a column to sort by, and the default sorting is in ascending order. Explicitly adding desc will sort in descending order. Using asc will also sort in ascending order select name, math from exam_result order by math desc; +------+------+ | name | math | +------+------+ | tom | 98.0 | | lim | 98.0 | | huy | 85.0 | | tim | 84.0 | | jum | 78.0 | | sun | 73.0 | | ming | 65.0 | +------+------+ NULL data is sorted as smaller than any value, appearing at the top in ascending order and at the bottom in descending order Sorting can also be performed by specifying multiple columns select * from exam_result order by math desc, chinese desc; +------+------+---------+------+---------+ | id | name | chinese | math | english | +------+------+---------+------+---------+ | 3 | lim | 88.0 | 98.0 | 90.0 | | 1 | tom | 67.0 | 98.0 | 56.0 | | 5 | huy | 55.5 | 85.0 | 45.0 | | 4 | tim | 82.0 | 84.0 | 67.0 | | 2 | jum | 87.5 | 78.0 | 77.0 | | 6 | sun | 70.0 | 73.0 | 78.5 | | 7 | ming | 75.0 | 65.0 | 30.0 | +------+------+---------+------+---------+ When sorting multiple columns, if the first column cannot distinguish the size, sort by the second column. 2.7 Conditional Query: WHERE Comparison Operators
Logical operators:
Notice:
Example: Basic query: -- Query students who failed English and their English scores (< 60) select name, english from exam_result where english < 60; -- Query students whose Chinese scores are better than their English scores select name, chinese, english from exam_result where chinese > english; -- Query students whose total scores are below 200 select name, chinese + math + english as total from exam_result where chinese + math + english < 200; AND and OR: -- Query students whose Chinese scores are greater than 80 and whose English scores are greater than 80 select * from exam_result where chinese > 80 and english > 80; -- Query students whose Chinese scores are greater than 80 points or whose English scores are greater than 80 points select * from exam_result where chinese > 80 or english > 80; Regarding the priority issue, and takes precedence over or. Range query: 1.BETWEEN … AND … -- Query the students whose Chinese scores are between [80, 90] and their Chinese scores select name, chinese from exam_result where chinese BETWEEN 80 AND 90; select name, chinese, from exam_result where chinese >= 80 and chinese <= 90; IN -- Query the students whose math scores are 58, 59, 98 or 99 and their math scores select name, math from exam_result where math in (58, 59, 98, 99); Fuzzy query: LIKE select name from exam_result where name like 't%'; +------+ | name | +------+ | tom | | tim | +------+ % is a wildcard character that can be used to replace any number of characters. t% Find the string that starts with t %t finds strings ending with t %t% finds the words containing t In addition to %, there is also _ (_ can only represent one character~) select name from exam_result where name like 't__'; +------+ | name | +------+ | tom | | tim | +------+ Wildcards can also be used for fuzzy queries on numbers select name, chinese from exam_result where chinese like '%8%'; +------+---------+ | name | chinese | +------+---------+ | jum | 87.5 | | lim | 88.0 | | tim | 82.0 | +------+---------+ Notice: Fuzzy query seems to be more useful, but the actual execution efficiency is low NULL query: IS [NOT] NULL select name from exam_result where id id not null; 2.8 Paginated Query: LIMIT -- Initial data table select * from exam_result; +------+------+---------+------+---------+ | id | name | chinese | math | english | +------+------+---------+------+---------+ | 1 | tom | 67.0 | 98.0 | 56.0 | | 2 | jum | 87.5 | 78.0 | 77.0 | | 3 | lim | 88.0 | 98.0 | 90.0 | | 4 | tim | 82.0 | 84.0 | 67.0 | | 5 | huy | 55.5 | 85.0 | 45.0 | | 6 | sun | 70.0 | 73.0 | 78.5 | | 7 | ming | 75.0 | 65.0 | 30.0 | +------+------+---------+------+---------+ -- The first three records select * from exam_result limit 3; +------+------+---------+------+---------+ | id | name | chinese | math | english | +------+------+---------+------+---------+ | 1 | tom | 67.0 | 98.0 | 56.0 | | 2 | jum | 87.5 | 78.0 | 77.0 | | 3 | lim | 88.0 | 98.0 | 90.0 | +------+------+---------+------+---------+ -- Three records starting from the third one select * from exam_result limit 3 offset 3; +------+------+---------+------+---------+ | id | name | chinese | math | english | +------+------+---------+------+---------+ | 4 | tim | 82.0 | 84.0 | 67.0 | | 5 | huy | 55.5 | 85.0 | 45.0 | | 6 | sun | 70.0 | 73.0 | 78.5 | +------+------+---------+------+---------+ offset indicates the number of entries to start searching from , offset can be omitted select * from exam_result limit 3 , 4; +------+------+---------+------+---------+ | id | name | chinese | math | english | +------+------+---------+------+---------+ | 4 | tim | 82.0 | 84.0 | 67.0 | | 5 | huy | 55.5 | 85.0 | 45.0 | | 6 | sun | 70.0 | 73.0 | 78.5 | | 7 | ming | 75.0 | 65.0 | 30.0 | +------+------+---------+------+---------+ 3. Update- Add 30 points to the math scores of the bottom three students in total update exam_result set math = math + 30 order by chinese + math + english limit 3; update without adding conditions, it can be applied to all 4. Deletedelete from [table name]; -- Delete Ming's test score delete from exam_result where name = 'ming'; -- Delete the entire table delete from exam_result; If no conditions are specified, the entire table will be deleted (this is different from the drop command). After delete, the table is null, and after drop, the table does not exist. 5. Commonly added-- Single row insert insert into [table name] (field 1, ..., field N) values (value1, ...,value N); -- insert into [table name](field 1, ..., field N) values (value1, ...), (value2, ...), (value3, ...); Query --Full table query select * from [table name]; --Specify column query select [column name 1, column name 2,...] from [table name]; --Query expression field select [expression 1, expression 2,...] from [table name]; --alias select --DISTINCT select distinct [field] from [table name]; -- Sorting ORDER BY select * from [table name] order by [sort field]; -- Conditional query WHERE -- (1) Comparison operator (2) BETWEEN ... AND ... (3) IN (4) IS NULL (5) LIKE (6) AND (7) OR (8)NOT select * from [table name] where [condition]; Revise update [table] set [modification content 1, modification content 2, ....] where [condition]; delete delete from [table name] where [condition]; SummarizeThis is the end of this article about MySQL table additions, deletions, modifications and queries. For more relevant MySQL table additions, deletions, modifications and queries, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Xhtml special characters collection
>>: Summary of ways to implement single sign-on in Vue
The role of the interface: Interface, in English:...
Today I learned a new CSS special effect, the wav...
View system help help contents mysql> help con...
If your DOCTYPE is as follows: Copy code The code ...
1. First create the file (cd to the directory whe...
This article uses examples to illustrate the MySQ...
Table of contents Preface What is Hot Change Coco...
Table of contents 1. Scene introduction 2 Code Op...
The temperament of a web front-end website is a fe...
By adding the current scroll offset to the attrib...
Table of contents MySQL crash recovery process 1....
Using the CSS float property correctly can become...
vue+el-upload multiple files dynamic upload, for ...
This article shares the specific code of js to re...
Be careful when listening for events that are tri...