MYSQL Based (a) data sorting (ORDER BY and LIMIT)

This is the first day of review MYSQL

  • Sort alphabetically
select prod_name 
from products
order by prod_name;

Selection of prod_name prod_name sorted alphabetically

 

  • Sort by multiple columns
select prod_id, prod_price, prod_name
from products
order by prod_price, prod_name;

To sort by multiple columns (as done when just select multiple columns) just specify the column name, column names separated by commas between can. Important in understanding the plurality of sorting, sorting in full in the order specified. In other words, the output of the above-described examples, the best value will be the same in a plurality of rows prod_price products sorted according prod_name. If all prod_price column values are unique, it is not in accordance with prod_name sort .

 

  • Specify the sort direction
select prod_id, prod_price, prod_name
from products
order by prod_price desc;

Data sorting is not limited to ascending order (A-> Z). This is the default sort in ascending order, you may also be used in descending order ORDER BY. Need to add DESC keyword.

  • LIMIT combined with ORDER BY queries
select prod_price
from products
order by prod_price DESC
limit 1;

Use ORDER BY and LIMIT combination, can be found in the highest or lowest values ​​of a column.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Released three original articles · won praise 0 · Views 54

Guess you like

Origin blog.csdn.net/qq_38336343/article/details/104467994