MYSQL single table--addition, deletion, modification, and query operation

1 Insert data

The INSERT INTO statement can be written in two forms.
The first form does not need to specify the name of the column into which the data is to be inserted, just provide the value to be inserted:
INSERT INTO table_name
VALUES (value1, value2, value3,...);

Note:
 1 The value must be consistent with the field data type in the table, and the number must be consistent.
2 In mysql, the string and date can be represented by ""''
3 Numerical value and string can be used in common, and it is recommended to use it in a non-universal way.

The second form needs to specify the column name and the value to be inserted:
INSERT INTO table_name (column1,column2,column3,…)
VALUES (value1,value2,value3,…);
1 The value must follow (column1,column2,column3,…) be consistent

– Insert data
– Do not add table fields, so the data in values ​​must be consistent with the order of the fields in the table structure
– mysql string and date data can be represented by ”“”
– Strings and integers can be used in mysql , It is recommended not to
insert into student values("7",2,'男','Beijingxx',1);

– Add specified fields
– Then the data in values ​​must be consistent with the number of data types in the specified field order
insert into student(s_name,s_sex,s_address,c_id) values("root",'男','北京xx', 1,);

– Copy the data in the table
insert into student(s_name,s_sex,s_address,c_id) select s_name,s_sex,s_address,c_id from student;

2 Update data

SQL UPDATE 语法
UPDATE table_name
SET column1=value1,column2=value2,…
WHERE some_column=some_value;

Please pay attention to the WHERE clause in the SQL UPDATE statement!
The WHERE clause specifies which record or records need to be updated. If you omit the WHERE clause, all records will be updated!

3 Delete data

SQL DELETE 语法
DELETE FROM table_name
WHERE some_column=some_value;

Please pay attention to the WHERE clause in the SQL DELETE statement!
The WHERE clause specifies which record or records need to be deleted. If you omit the WHERE clause, all records will be deleted!

4 Query data

In database operations, single-table query is to query data in a table. The detailed syntax is:

select distinct field 1, field 2... from table name
where filter conditions before grouping
group by grouping field
having filter conditions after grouping
order by the
number of items displayed in the sorting field limit;

The grammar is such an order, but its execution order is not executed from the order of the grammar, but such an order.
select—>distinct —>from—>where—>group by—>having—>order by—>limit


-Query the name of the book, author select b_name, b_authors from book;

– The query book number is 1 * represents all the fields
select * from book where b_id=1;


-Remove duplicate field data select distinct b_authors from book;

– Query the information of the book, the author is not equal to tom
select * from book where b_authors = "tom";

– Query book information, the price is greater than 20
select * from book where b_price >=30;


/*
BETWEEN operator is used to select a value in the data range between two values.
The BETWEEN operator selects a value in the data range between two values. These values ​​can be numeric, text, or date.
*/
– Query the information of the book, the price is between 30 and 50 50>=value>=30

select * from book where b_price between 30 and 50;
select * from book where b_price >=30 and b_price <= 50 ;

The /*
IN operator allows you to specify multiple values ​​in the WHERE clause.
*/
– Query book information, the price is equal to 35, 36, 37
select * from book where b_price in (35,36,37);
select * from book where b_price=35 or b_price=36 or b_price=37;

/*
SQL AND & OR operator
and and
or or
*/
-query the information of the book, the author is tom and the price is equal to 35
select * from book where b_authors="tom" and b_price=35;

– Query book information, the author is tom or the price is greater than 50
select * from book where b_authors="tom" or b_price>50;

/*
like fuzzy query
% substitute 0 or more characters
_ substitute one character
*/
– The search book title contains the word "宝"
-"%通"; ends with a pass, %宝% contains the word treasure, "j%" Start with j
select * from book where b_name like “j%”;

/**
as alias
Tables, fields, and subquery results can all have aliases

Function:
1 Convenient to view data
2 Convenient to connect multiple tables
*/
select b.b_name as "Book Title", b.b_authors as "Author" from book as b;

/*
limit 分页
MySQL LIMIT
Oracle ROWNUM
sql server top
*/
– insert into book(b_name,b_authors,b_price,b_date,b_type,b_number) select b_name,b_authors,b_price,b_date,b_type,b_number from book;

– Limit 3 3 means to fetch the first 3 records
select * from book where b_authors = "tom" limit 3;
– limit 0 ,10 start from the first and fetch 10 records from the next (starting from 0)
select * from book where b_authors = "tom" limit 3 ,10;

/*
order by sorting
Default natural sorting ascending order
descending desc
*/

– Descending order according to the price of the
book select * from book order by b_price desc;


– 多列排序
select * from book order by b_price desc,b_date desc;

/*
group by grouping
features:
1 group by can remove repeated data
2 purposes to find the average, maximum and minimum values. . .
3 group by is generally used with functions, but functions can be used alone.
4 Use where to filter before grouping, and then HAVING

When the group by appears in the sql statement, which fields can only be placed after the select?
1 Grouped fields
2 Fields with fields as function parameters

A function
can be thought of as a factory, where materials need to be placed, and the result is the product.
Function needs to be placed in parameters, and the return value is the result

The reason for adding the HAVING clause in SQL is that the WHERE keyword cannot be used with aggregate functions.
The HAVING clause allows us to filter the grouped data
*/

– According to the average price of each book
select avg(b_price) as avg_price ,b_type,b_name from book group by b_type;

– In descending order according to the average price of each book
select avg(b_price) as avg_price ,b_type,b_name from book group by b_type order by avg_price desc;

– According to the average price of each book is greater than 36 and then in descending order
select
avg(b_price) as avg_price,
b_type
from
book
group by b_type
having avg_price>36
order by avg_price desc;

– Books with a publication date after 2019-09-01 will be in descending order according to the average price of each book being greater than 36

select
avg(b_price) as avg_price,
b_type
from
book
where b_date > “2019-09-01”
group by b_type
having avg_price > 36
order by avg_price desc ;

– For books with a publication date after 2019-09-01, the average price of each book is greater than 36, and then the largest one
select
avg(b_price) as avg_price,
b_type
from
book
where b_date> “2019-09-01”
group by b_type
having avg_price> 36
order by avg_price desc limit 1;

Guess you like

Origin blog.csdn.net/weixin_47580822/article/details/112777208