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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles