Query type of SQL statement

Table of contents

1. Query the table content and add one more column

2. Add new column names to the data table

 3. Subquery

4. Aggregation function

1. Average calculation AVG command

2. Sum command to calculate sum

3. count command

4. MAX command

 5. MIN command

5. Multi-table query

1. Inner join query

 2. External connection query

1. Left outer join query

 2. Right outer join query

 6. Summary


1. Query the table content and add one more column

Enter select * from mpy; view the table contents

2. Add new column names to the data table

Enter alter table mpy add ip1 int(20); (modify a table name, add means I want to add a new column name, and add attributes after the column name)

Enter select * from mpy again; query

 3. Subquery

select * from test where age > (select avg(age) from test);

(When the age is greater than the average value I queried, the content of the age information is retained). These are two SQL statements. However, when comparing operators, they are not one value, but are obtained through SQL statement query. Comes worth it.

4. Aggregation function

1. Average calculation AVG command

select avg(colume1) from test;

Create a data table called classes and view 

Enter the select avg(age) from banji; command to calculate the average of age

 The average was successfully calculated to be 39.27

2. Sum command to calculate sum

import select sum(age) from banji;

 Successfully obtained the sum of the total number 432

3. count command

Used to count the number of rows in a specified column, excluding non-empty rows

select count(age) from banji;

Successfully calculated 11 columns

4. MAX command

Maximum value in specified column

select max(age) from banji;

 5. MIN command

Minimum value in specified list

select min(age) from banji;

5. Multi-table query

1. Inner join query

First check the content of the two tables that are incorrectly communicated with each other.

The input content mpy represents the first table, banji represents the second table, and .name represents the column content in the table.

 2. External connection query

1. Left outer join query

Enter the following command (data that matches the table on the left is displayed)

mysql> select *
    -> from banji
    -> left join mpy
    -> on banji.name = mpy.name; 

 

 2. Right outer join query

Enter the following command (data that matches the table on the right is displayed)

mysql> select *
    -> from mpy
    -> left join banji
    -> on mpy.name = banji.name; 

 6. Summary

Database content is indispensable for the current Internet environment. Addition, deletion, modification, and multi-table query are very important links. There is a high probability that these questions will be asked in interviews, so you must practice more and stick to them. Have a firm grasp.

Guess you like

Origin blog.csdn.net/Mapinyi666/article/details/131945882