Mysql add, delete, modify query records
Before explaining the query, I prepared a data table for everyone. This table stores the bank's balance and basic information about the user.
We defined a table structure named money.
The statement to create the table is as follows:
CREATE TABLE
money
(
id
INT NOT NULL AUTO_INCREMENT ,
username
VARCHAR(50) NOT NULL ,
balance
FLOAT NOT NULL ,
province
VARCHAR(20) NOT NULL ,
age
TINYINT UNSIGNED NOT NULL ,
sex
TINYINT NOT NULL ,
PRIMARY KEY (id
(10))
) ENGINE = InnoDB CHARACTER SET utf8;
The table structure and data are displayed as follows:
username | balance | province | age | sex | |
---|---|---|---|---|---|
Wang Baoqiang | 120.02 | 上海 | 29 | 1 | |
Fan Bingbing | 260.23 | Shandong | 40 | 0 | |
黄晓明 | 150.86 | Shandong | 40 | 1 | ##4 |
810 | Liaoning | 27 | 1 | ##5 | |
20.15 | Heilongjiang | 43 | 0 | ##6 | Jackie Chan |
Shandong | 63 | 1 | 7 | Yang Mi | |
北京 | 30 | 0 | ##8 | Liu Shishi | |
Beijing | 29 | 1 | ##9 | Liu Yan | 23.4 |
36 | 0 | ##10 | Zhao Benshan | 3456 | |
63 | 1 | ##11 | 王峰 | 34.32 | Beijing |
1 | ##12 | Guo Degang | 212 | 天津 | |
1 | Note: | balance refers to the balance | province refers to the provinceBasic query |
Detailed explanation
Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Example description | Query the money table All results in all fields | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Category | Detailed explanation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
select field from table; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
select id,username, balance from money; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Query id,username, in the money table All results in the balance field |
Category | Detailed explanation |
---|---|
Basic syntax | select distinct field from table; |
Example | select distinct age deptno from money; |
Example description | Query all results with unique age in the money table |
+--------+
| deptno |
+--------+
| 29 |
| 40 |
| 27 |
| 43 |
| 63 |
| 30 |
| 36 |
| 44 |
+--------+
8 rows in set (0.00 sec )
Detailed explanation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
select field from table where where condition; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
select * from money where age = 29; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Query all results with age 29 in the money table |
Description | |
---|---|
is greater than | |
Less than | |
Greater than or equal to | |
Less than or equal to | ##!= |
= | |
You can also use or, and and other logical operators to perform multi-condition joint queries for multiple conditions
and | |
Instructions | |
| id | username | balance | province | age | sex |+----+-----------+---------+----------+-----+----- +
| 1 | Wang Baoqiang | 120.02 | Hubei | 29 | 1 |
+----+-----------+---------+----------+-----+----- +
1 row in set (0.00 sec)
Result set sorting
Example | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Example description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Keywords | Description |
---|---|
asc | Arrange in ascending order, from small to large (default) |
desc | Arrange in descending order, from large to small |
Use order by to sort the result set after the select comes out, where desc and asc are keywords in the sort order. desc means to sort by fields in descending order, and asc means to sort in ascending order. If no keyword is written, the default is to sort in ascending order.
mysql> select id,username, balance from money order by balance desc;
+----+-----------+-------- -+
| id | username | balance |
+----+-----------+---------+
| 10 | Zhao Benshan | 3456 |
| 4 | Jing Bairan | 810 |
| 8 | Liu Shishi | 456 |
| 6 | Jackie Chan | 313 |
| 2 | Fan Bingbing | 260.23 |
| 12 | Guo Degang | 212 |
| 3 | Huang Xiaoming | 150.86 |
| 7 | Yang Mi | 123 |
| 1 | Wang Baoqiang | 120.02 |
| 11 | Wang Feng | Liu Yan | 23.4 |
| 5 | Li Bingbing | 20.15 |
+----+-----------+---------+
12 rows in set (0.00 sec)
Detailed explanation | |
---|---|
select field from table order by field 1 sort keyword,... ...Field n desc|asc; | |
select id,username, balance from money order by balance desc,age asc; | |
Query the id, username, and balance fields in the money table, and sort them in descending order according to the balance. If the balance If they are all the same, then use age to sort in ascending order |
Category | Detailed explanation |
---|---|
Basic syntax | select field from table limit quantity; |
Example | select id,username, balance from money limit 5; |
Display the first five users |
Category | Detailed explanation |
---|---|
select field from table order by field keyword limit quantity | |
select id,username, balance from money order by balance desc limit 5; | |
Sort by money, display the top five richest users |
| id | username | balance |
+----+-----------+---------+
| 10 | Zhao Benshan | 3456 |
| 4 | Jing Boran | 810 |
| 8 | Liu Shishi | 456 |
| 6 | Jackie Chan | 313 |
| 2 | Fan Bingbing | 260.23 |
+----+----------+ ---------+
5 rows in set (0.00 sec)
Result set interval selection
Suppose I fetch 3 records starting from 0. I want to fetch 3 more records starting from the 3rd one. What should I do if I want to fetch 4 records starting from the 6th one?
At this time, you need to use the result set interval selection.
Detailed explanation | |
---|---|
select field from table limit offset, quantity | |
select id,username, balance from money limit 0,3; | |
Get three records starting from the first one |
Function | Description |
---|---|
sum | Sum |
count | Total statistics |
max | Maximum value |
min | Minimum value |
Average |
Note: Of course you know that other mysql functions can also be used. However, in actual work, it is rarely used in many large and medium-sized projects in large companies, and they all have dedicated counting servers. Because the calculation amount of MySQL itself is very large, in order to reduce the pressure, we usually leave the actual calculation tasks to the business server or other servers to complete.
Detailed explanation | |
---|---|
select function (field) from table | |
select count(id) from money | |
Query the total number of ids in the money table |
Category | Detailed explanation |
---|---|
Basic syntax | select * from table group by field |
Example | select * from money group by province; |
Example description | Group by region |
mysql> select * from money group by province;
+----+-----------+---------+------ -----+-----+-----+
| id | username | balance | province | age | sex |
+----+------- ----+---------+-----------+-----+-----+
| 7 | Yang Mi | 123 | Beijing | 30 | 0 |
| 12 | Guo Degang | 212 | Tianjin | 43 | 1 |
| 2 | Fan Bingbing | 260.23 | Shandong | 40 | 0 |
| 1 | Wang Baoqiang | 120.02 | Hubei | 29 | 1 |
| 9 | Liu Yan | 23.4 | Hunan | 36 | 0 |
| 4 | Jing Boran | 810 | Liaoning | 27 | 1 |
| 5 | Li Bingbing | 20.15 | Black Dragon Jiang | 43 | 0 |
+----+-----------+----------+-----------+-----+ -----+
Statistical grouping (category) total number:
mysql> select deptno, count(1) from emp group by deptno;
+----- ---+----------+
| deptno | count(1) |
+--------+----------+
| 1 | 1 1 |
| 2 | 5 |
| 3 | 1 1 |
| 5 | 4 |
+--------+----- -----+
4 rows in set (0.04 sec)
Count the number of provinces and then display them in groups
mysql> select count(province),province from money group by province;
+------------------+----------+
| count(province) | province |
+------------------+----------+
| 3 | Beijing |
| | | Tianjin |
| 3 | Shandong |
| 1 1 Hubei |
| 1 1 | Hunan |
| 2 | Liaoning |
| Heilongjiang |
+------------------+----------+
7 rows in set (0.00 sec)
Statistics based on grouping
with rollup is rarely used. This knowledge point is set to the understanding level.
Its main function is to count the grouped data and then perform a total count.
Category | Detailed explanation |
---|---|
select * from table group by field with rollup | |
select count(province),province from money group by province with rollup; | |
Count the number of groups again |
+------------------+----------+| count(province) | province |
The results are then filtered having
+------------------+----------+
| 3 | Beijing |
| | | Tianjin |
| 3 | Shandong |
| 1 1 Hubei |
| 1 1 | Hunan |
| 2 | Liaoning |
| Heilongjiang |
| 12 | NULL |
+------------------+----------+
8 rows in set (0.00 sec)
The having clause is similar to where but also different. They are both statements that set conditions.
having is the filtering group and where is the filtering record.
Detailed explanation | |
---|---|
select * from table group by field having conditions | |
select count(province) as result,province from money group by province having result >2; | |
Group regions and count the total, and display the grouped regions greater than 2 in the grouping results |
Keywords | Description |
---|---|
select | Selected columns |
from | Table |
where | Query conditions |
group by | Group attribute having group filter conditions |
order by | Sort attribute |
limit | Starting record position, take the number of records |
us Perform an overall use and query the money table fields: id, username, balance, province. It is required that id>1 and the balance be greater than 50. Use regions for grouping. We use the user ID to perform descending order, and only 3 items are allowed to be displayed.
Finally write the SQL statement as follows, and the query results are as follows:
Continuing Learningmysql> select id,username,balance,province from money where id > 1 and balance > 50 group by province order by id desc limit 3;
+----+-----------+---------+----------+
| id | username | balance | province |
+----+-----------+---------+----------+
| 12 | Guo Degang | 212 | Tianjin |
| 7 | Yang Mi | 123 | Beijing |
| 4 | Jing Boran | 810 | Liaoning |
+----+-----------+---------+----------+
3 rows in set (0.00 sec)
- Course Recommendations
- Courseware download
-
ElementaryImperial CMS enterprise imitation website tutorial
3048 people are watching -
ElementaryNewbies with zero foundation in WordPress build personal blogs and corporate websites
6743 people are watching -
ElementaryUltimate CMS zero-based website building instruction video
2724 people are watching -
ElementaryFront-end project-Shangyou [HTML/CSS/JS technology comprehensive practice]
3117 people are watching -
IntermediateVue3.0 from 0 to build a universal backend management system project practice
5351 people are watching -
ElementaryZero-based front-end course [Vue advanced learning and practical application]
2821 people are watching -
ElementaryWEB front-end tutorial [HTML5+CSS3+JS]
3506 people are watching -
ElementaryQuick introduction to apipost
2161 people are watching -
IntermediateVue3+TypeScript practical tutorial-enterprise-level project practice
3208 people are watching -
ElementaryLet's briefly talk about starting a business in PHP
17423 people are watching -
IntermediateVUE e-commerce project (front-end & back-end dual project actual combat)
3828 people are watching -
ElementaryApipost practical application [api, interface, automated testing, mock]
2265 people are watching
Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)