MySQL - data manipulation

Data manipulation:
1. insert:
Format 1 insert into table name (field 1, field 2,..) values ​​(value 1, value 2,...) Insert value into the specified column
Format 2 insert into table name values ​​(value 1, value 2,…) Insert values ​​into all columns in the table
Note: The number of fields must match the number and type of values. 1) The inserted value type must
match the column type.
2) The value length cannot exceed the column definition length
. : delete is used to delete records in the table. Format delete from table name. The above operation is to delete all records in the table. If you want to delete the specified record, you need to use the where. Delete from table name where condition. What is the difference between Delete and drop and truncate Drop table drop database It is used to delete a table or database. Delete and truncate are used to delete records. Delete from table name, Truncate table table name: they all delete all the data in the table. The difference is: delete deletes records line by line. Truncate destroys the table structure and recreates the table structure. Delete deletes are subject to transaction control, while truncate is not subject to transaction control.















3. update:
used to modify the data
format in the table update table name set field name = value, field name = value;
the above modification is to modify all the fields in the table. In development, it is generally conditional modification.
update table name set field name = value, field name = value where condition.
select:
select operation is used to query record
format 1. select * from table name query the value of all fields in the table
format 2: select field 1, field 2, .. from tablename Query the value of the specified field in the table.
Format 3 select distinct field1, field2,. . . The table name queries unique data.
Note: distinct is used to remove duplicates.
Format 4 The fields in the table can be directly operated to
find the total score of each of the three subjects in the three subjects
select name, chinese+math+english from exam;
Format 5 can alias the column or table
select name as name, (chinese+ math+English) as three subject scores from exam;
use as alias after column name or table name
select name name, (chinese+math+English) three subject scores from exam test score table;

 

The writing order of the Select statement:
Select * from table where condition group by field having condition order by . field.
The parsing order of the Select statement:
from - where - group by - having –select- order by


where clause:
1. relational operator > < >= <= = !=(<>)
2. logical operator and or not
3. between and
select * from exam where chinese between 60 and 90;
4. in operation
select * from exam where math in(65,75)
5. is null is not null
Note: null values ​​cannot be compared using =.
6.like operation Fuzzy query
can use two wildcard characters in fuzzy query: % _
% wildcard all
_ It wildcards a character

order by:
Order by field asc/desc, field asc/desc;
the default is asc ascending order desc is descending order
Note: The order by clause is always the last in the select statement.

 

Aggregate functions:
1. count It is used to count the number of rows in a column. Note: If the value of a row is null, it will not be counted; a method often used in development select count(*) from table;
2. sum It is used to count the sum of a certain column; Note: If there is a column value in Null, sum can be treated as 0.
3. avg It is used for averaging.
4. max min Find the maximum value in a column, the minimum value

group by:
After group by, you can use having to filter the conditions. Use having must use group by.
1. having is to filter after grouping, where is to filter before grouping.
2. The grouping function can be used after having, and the grouping function cannot be used after where.
You can use having to replace where. Note that it can only be used in grouping operations, because having must appear after group by.


Regarding the operation of null:
in mysql, if nul is operated with any value, the result is null.
In development, when we operate on null, we may treat it as 0. What should we do?
Provide a value in mysql The function is called ifnull   
select ifull(english,0) from exam;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326480721&siteId=291194637