Table of Contents
1. Query records
2. Sorting
3. Limitation
4. Aggregation
Home Database Mysql Tutorial How to perform data query in mysql

How to perform data query in mysql

Mar 25, 2021 pm 04:45 PM
mysql data query

<p>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. </p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/062/605c461923b8f256.jpg" class="lazy" alt="How to perform data query in mysql" ></p> <p>First we create a data table students. This operation is based on this data table. The data of this data table is as follows: <br></p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/203/886/414/1616660173334521.png" class="lazy" title="1616660173334521.png" alt="How to perform data query in mysql"></p> <h2 id="strong-Query-records-strong"><strong>1. Query records</strong></h2><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>select distinct 字段1,字段2 from 表名;</pre><div class="contentsignin">Copy after login</div></div><p>Among them: </p><ul style="list-style-type: disc;"><li><p>field: refers to the field that needs to be queried, such as in the data table id or age; </p></li><li><p> Table name: It is the data table we operate on, for example, I am operating the table students; </p></li></ul><p><strong>As long as any field between field 1 and field 2 is different, it will be selected. It is generally used for <code>distinct</code> to filter only one field; </strong></p><p>At the same time, this statement can also be used for a certain Query under these conditions, <strong> conditional query comparison symbols: <code>=</code>,<code><</code>,<code>></code>,<code>>=</code>, <code><=</code>,<code>!=</code> and other comparison operators, you can use <code>or</code> <code>and</code> and other </strong>## between multiple conditions #You can use <strong> in this statement. </strong></p>You can see the example: <p></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/296/473/935/1616660393871606.png" class="lazy" title="1616660393871606.png" alt="How to perform data query in mysql"/></p><h2></h2><h2 id="Sorting-strong-strong">2. Sorting<strong></strong></h2>You can use sql statements Sort the data in ascending and descending order. <p></p>Ascending order: <p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>select * from 表名 order by 字段名 asc;</pre><div class="contentsignin">Copy after login</div></div></p>Descending order: <p><br/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>select * from 表名 order by字段名 desc;</pre><div class="contentsignin">Copy after login</div></div></p>In sql, <p>the default is ascending order when sorting. <strong></strong><br/></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/889/560/426/1616660920680907.png" class="lazy" title="1616660920680907.png" alt="How to perform data query in mysql"/></p><h2 id="Limitation-strong-strong">3. Limitation<strong></strong></h2>Add <p><code>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! </code></p>If only the number 1 is written after <p>limit<code>, then one record will be taken by default. </code></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/536/148/604/1616661042879597.png" class="lazy" title="1616661042879597.png" alt="How to perform data query in mysql"/></p><h2 id="Aggregation-strong-strong">4. Aggregation<strong></strong></h2> Users need to perform some summary operations, which requires sql aggregation operations. <p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>select sum(字段名) from 表名;</pre><div class="contentsignin">Copy after login</div></div></p><p>The sum of data can be achieved through <strong>sum<code>. </code></strong><br/></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/482/120/916/1616661304589370.png" class="lazy" title="1616661304589370.png" alt="How to perform data query in mysql"/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>select count(*|字段名) from 表名;</pre><div class="contentsignin">Copy after login</div></div></p><p>The counting of fields can be completed through count. The counting results of using * or field names are the same. <strong></strong><br/></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/878/522/306/1616661568908733.png" class="lazy" title="1616661568908733.png" alt="How to perform data query in mysql"/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>select max(字段名) from 表名;</pre><div class="contentsignin">Copy after login</div></div></p><p>max can find the maximum value in the data. <strong>In this data table, the maximum value is 40, so the output result is 40. </strong><br/></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/200/528/683/1616661728542249.png" class="lazy" title="1616661728542249.png" alt="How to perform data query in mysql"/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>select min(字段名) from 表名;</pre><div class="contentsignin">Copy after login</div></div><p>min can find the minimum value in the data. <strong>In this data table, the minimum value is 10, so the output result is 10. </strong></p> <p><strong><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/101/796/991/1616661809887684.png" class="lazy" title="1616661809887684.png" alt="How to perform data query in mysql"></strong></p> <p> Recommended tutorial: <strong>mysql video tutorial<a href="https://www.php.cn/course/list/51.html" target="_self"></a></strong></p>

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles