Home  >  Article  >  Database  >  How to perform data query in mysql

How to perform data query in mysql

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-03-25 16:45:425313browse

We have learned about the addition, deletion, modification and query operations of the database before, but we only have a simple understanding of the query operations of the database. Let's follow the editor to learn other operations of database query records.

How to perform data query in mysql

First we create a data table students. This operation is based on this data table. The data of this data table is as follows:

How to perform data query in mysql

1. Query records

select distinct 字段1,字段2 from 表名;

Among them:

  • field: refers to the field that needs to be queried, such as in the data table id or age;

  • Table name: It is the data table we operate on, for example, I am operating the table students;

As long as any field between field 1 and field 2 is different, it will be selected. It is generally used for distinct to filter only one field;

At the same time, this statement can also be used for a certain Query under these conditions, conditional query comparison symbols: ,,<code>>,<code>>=, ,<code>!= and other comparison operators, you can use or and and other ## between multiple conditions #You can use in this statement.

You can see the example:

How to perform data query in mysql

2. Sorting

You can use sql statements Sort the data in ascending and descending order.

Ascending order:

select * from 表名 order by 字段名 asc;

Descending order:


select * from 表名 order by字段名 desc;

In sql,

the default is ascending order when sorting.

How to perform data query in mysql

3. Limitation

Add

limit at the end of the statement Number 1, number 2<strong></strong> are used to limit the number of queries. The number 1 represents which record to start fetching (starting from 0), and the number 2 represents how many records to fetch!

If only the number 1 is written after

limit, then one record will be taken by default.

How to perform data query in mysql

4. Aggregation

Users need to perform some summary operations, which requires sql aggregation operations.

select sum(字段名) from 表名;

The sum of data can be achieved through sum.

How to perform data query in mysql

select count(*|字段名) from 表名;

The counting of fields can be completed through count. The counting results of using * or field names are the same.

How to perform data query in mysql

select max(字段名) from 表名;

max can find the maximum value in the data. In this data table, the maximum value is 40, so the output result is 40.

How to perform data query in mysql

select min(字段名) from 表名;

min can find the minimum value in the data. In this data table, the minimum value is 10, so the output result is 10.

How to perform data query in mysql

Recommended tutorial: mysql video tutorial

The above is the detailed content of How to perform data query in 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