Home > Database > Mysql Tutorial > body text

Sharing of data filtering methods in MySQL

WBOY
Release: 2023-06-15 21:38:48
Original
3598 people have browsed it

MySQL is one of the most commonly used relational database management systems at present. It provides a variety of data filtering methods to obtain the required data from the database. This article will share commonly used data filtering methods in MySQL for readers’ reference and learning.

1. WHERE statement

The WHERE statement is the most basic and commonly used data filtering method in MySQL. It filters out the required data from the table based on specified conditions. For example:

SELECT * FROM table_name WHERE column_name = value;
Copy after login

Among them, table_name is the name of the table that needs to be queried, column_name is the name of the column that needs to be filtered, and value is the value that needs to be filtered. This statement will return all data in the table_name table where the column_name column value is equal to value.

The WHERE statement can also use other conditional operators, such as greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), not equal to (!= ), LIKE, etc.

2. ORDER BY statement

The ORDER BY statement is used to sort the query results according to the specified column or expression. For example:

SELECT * FROM table_name ORDER BY column_name ASC/DESC;
Copy after login

Among them, table_name is the name of the table that needs to be queried, column_name is the name of the column that needs to be sorted, ASC means ascending order, and DESC means descending order. This statement will return all the data in the table_name table, sorted in ascending or descending order of the column_name column.

ORDER BY statement can also be sorted by multiple columns at the same time. For example:

SELECT * FROM table_name ORDER BY column_1 ASC, column_2 DESC;
Copy after login

This statement will be sorted in ascending order of column_1 column. If the column_1 column is the same, it will be sorted in ascending order of column_2 column. descending sort.

3. GROUP BY statement

The GROUP BY statement is used to group query results according to one or more columns and calculate the aggregate value of each group. For example:

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
Copy after login

Among them, table_name is the name of the table that needs to be queried, column_name is the name of the column that needs to be grouped, and COUNT(*) means counting the number of rows in each group. This statement will return the number of rows in each group in the table_name table grouped by the column_name column.

The GROUP BY statement can also use other aggregate functions, such as MAX, MIN, AVG, SUM, etc., for example:

SELECT column_name, MAX(value) FROM table_name GROUP BY column_name;
Copy after login

This statement will return the table_name table after grouping according to the column_name column. The maximum value of the group value column.

4. HAVING statement

The HAVING statement is similar to the WHERE statement, but is used to filter groups after the GROUP BY statement. For example:

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING COUNT(*) > 10;
Copy after login

Among them, table_name is the name of the table that needs to be queried, column_name is the name of the column that needs to be grouped, and COUNT(*) means counting the number of rows in each group. This statement will return each set of data in the table_name table that is grouped by the column_name column and has a number of rows greater than 10.

HAVING statement can use conventional WHERE conditional operators, such as greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), not equal to (! =), LIKE, etc.

5. LIMIT statement

The LIMIT statement is used to limit the number of rows returned by query results. For example:

SELECT * FROM table_name LIMIT 10;
Copy after login

Among them, table_name is the name of the table to be queried, and LIMIT 10 means that only the first 10 rows of data are returned. This statement will return the first 10 rows of data in the table_name table.

The LIMIT statement can also specify the starting position and number of rows of the returned data, for example:

SELECT * FROM table_name LIMIT 10 OFFSET 20;
Copy after login

This statement will start from row 21 in the table_name table and return 10 rows of data.

6. DISTINCT keyword

The DISTINCT keyword is used to return query results after deduplication. For example:

SELECT DISTINCT column_name FROM table_name;
Copy after login

Among them, table_name is the name of the table that needs to be queried, and column_name is the name of the column that needs to be deduplicated. This statement will return all the values ​​in the column_name column in the table_name table after deduplication.

7. IN keyword

The IN keyword is used to specify a list of values ​​and return query results that satisfy any value in the list. For example:

SELECT * FROM table_name WHERE column_name IN (value1, value2, value3);
Copy after login

Among them, table_name is the name of the table that needs to be queried, column_name is the name of the column that needs to be filtered, and value1, value2, and value3 are the values ​​that need to be matched. This statement will return all data in the table_name table whose column_name column matches any one of value1, value2, and value3.

8. NOT IN keyword

The NOT IN keyword is the opposite of the IN keyword, returning query results that do not satisfy any value in the list. For example:

SELECT * FROM table_name WHERE column_name NOT IN (value1, value2, value3);
Copy after login

Among them, table_name is the name of the table that needs to be queried, column_name is the name of the column that needs to be filtered, and value1, value2, and value3 are the values ​​that need to be matched. This statement will return all data in the table_name table where the column_name column does not match any value among value1, value2, and value3.

The above are commonly used data filtering methods in MySQL. Different method combinations can be selected in different scenarios. Readers can use it flexibly according to actual needs to improve the efficiency and accuracy of data filtering.

The above is the detailed content of Sharing of data filtering methods in MySQL. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!