Home >Database >Mysql Tutorial >Commonly used basic operation syntax of mysql (6) - conditional query for sorting data and limiting the number of results [command line mode]

Commonly used basic operation syntax of mysql (6) - conditional query for sorting data and limiting the number of results [command line mode]

黄舟
黄舟Original
2017-03-03 14:09:121424browse

1. Use order by to sort the query results, asc ascending order, desc descending order:


You can also specify multiple field names and after order by Sorting method for multi-level sorting:


2. Use limit to limit the number of query results:


The 0 in the above picture represents the starting position of the query, which can also be understood as the skipped quantity; the 2 in the above picture represents the quantity that needs to be queried. There are 3 pieces of data in this table. Because the number of pieces is limited to 2, the actual result is only two. In addition, the initial position here is 0. In fact, you don’t need to write it. The default is 0:


There is another situation here, that is, the number of specified query results may be is greater than the actual number in the table. At this time, the return result will return all the data that meets the conditions in the table. For example, there are 3 pieces of data in my table. If the specified number is 4, the actual query results are still 3 pieces:



where conditions, order by sorting and limit can be used in combination, and are often used in combination in actual application scenarios:


The above is the content of the common basic operation syntax of mysql (6) - conditional query [command line mode] for sorting data and limiting the number of results. For more related content, please pay attention to PHP Chinese website (www.php.cn)!


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