Home  >  Article  >  Database  >  How to query mysql

How to query mysql

PHPz
PHPzOriginal
2023-05-13 19:59:063358browse

MySQL is an open source relational database management system. During development, it is often necessary to query data from the database to meet business needs. MySQL provides a rich set of statements to query data. In this article, we will delve into how to query the MySQL database.

1. Basic syntax

The basic syntax of MySQL query statement is as follows:

SELECT column_name1, column_name2, ... FROM table_name WHERE condition;

Among them, the SELECT keyword is used to specify the column to be queried, and the FROM keyword is used to specify The table to be queried, and the WHERE clause is used to specify the conditions of the query.

For example, if we want to query all the information of a user named "Zhang San", we can use the following statement:

SELECT * FROM users WHERE name='张三';

This statement will query the user named "Zhang San" all information.

2. Specify the columns to be queried

When using the SELECT statement to query the database, you can use "*" to indicate querying all columns, or you can specify the columns to be queried. For example:

SELECT name, age, gender FROM users;

This statement will query the "name", "age" and "gender" columns of all users.

3. Query all records

If you want to query all records in the table, you can use the following statement:

SELECT * FROM table_name;

This statement will query all records in the table.

4. Use the WHERE clause

The WHERE clause is used to specify the conditions of the query. For example, if we want to query all users whose "age" is greater than 18 years old, we can use the following statement:

SELECT * FROM users WHERE age > 18;

This statement will query all users whose age is greater than 18 years old.

5. Use AND and OR keywords

When querying, you can use AND and OR keywords to combine conditions. For example, if we want to query all users who are between 18 and 30 years old and whose gender is female, we can use the following statement:

SELECT * FROM users WHERE age >= 18 AND age <= 30 AND gender = '女';

This statement will query all users who are between 18 and 30 years old. Users whose gender is female.

6. Use the IN operator

The IN operator is used to specify a set of one or more conditions, for example:

SELECT * FROM users WHERE name IN ('张三', '李四', '王五');

This statement will query all names named "Zhang Users of "Three", "John Four" or "Wang Wu".

7. Use the LIKE operator

The LIKE operator is used to search for values ​​containing the specified string. For example, if we want to query all users whose names begin with "Zhang", we can use the following statement:

SELECT * FROM users WHERE name LIKE '张%';

This statement will query all users whose names begin with "Zhang".

8. Use the ORDER BY clause

The ORDER BY clause is used to specify the sort order of query results. For example, if we want to query all users in order of age from small to large, we can use the following statement:

SELECT * FROM users ORDER BY age ASC;

This statement will query all users and sort them in order of age from small to large.

9. Use the LIMIT clause

The LIMIT clause is used to limit the number of query results. For example, if we only want to query the first 10 pieces of data, we can use the following statement:

SELECT * FROM users LIMIT 10;

This statement will query the first 10 user records.

10. Use the GROUP BY clause

The GROUP BY clause is used to group query results according to specified columns. For example, if we want to calculate the average age of all users grouped by gender, we can use the following statement:

SELECT gender, AVG(age) FROM users GROUP BY gender;

This statement will group by gender and calculate the average age of the users in each group.

The above is the basic syntax and operation of MySQL query. Learning these statements can easily query the data in the database. In practical applications, different statements can be used to complete more complex query operations according to needs.

The above is the detailed content of How to query mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:rootforgot password mysqlNext article:rootforgot password mysql