<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="https://img.php.cn/upload/article/000/000/062/605c461923b8f256.jpg" 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="https://img.php.cn/upload/image/203/886/414/1616660173334521.png" title="1616660173334521.png" alt="How to perform data query in mysql"></p>
<h2><strong>1. Query records</strong></h2><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:sql;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="https://img.php.cn/upload/image/296/473/935/1616660393871606.png" title="1616660393871606.png" alt="How to perform data query in mysql"/></p><h2></h2><h2 style="max-width:90%">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:sql;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:sql;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="https://img.php.cn/upload/image/889/560/426/1616660920680907.png" title="1616660920680907.png" alt="How to perform data query in mysql"/></p><h2>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="https://img.php.cn/upload/image/536/148/604/1616661042879597.png" title="1616661042879597.png" alt="How to perform data query in mysql"/></p><h2>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:sql;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="https://img.php.cn/upload/image/482/120/916/1616661304589370.png" title="1616661304589370.png" alt="How to perform data query in mysql"/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:sql;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="https://img.php.cn/upload/image/878/522/306/1616661568908733.png" title="1616661568908733.png" alt="How to perform data query in mysql"/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:sql;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="https://img.php.cn/upload/image/200/528/683/1616661728542249.png" title="1616661728542249.png" alt="How to perform data query in mysql"/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:sql;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="https://img.php.cn/upload/image/101/796/991/1616661809887684.png" 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!