SQL statements for linux --- ORDER BY, concise tutorial --- order
So far, we have learned how to useSELECTAndWHEREThese two commands capture data from the table. However, we have not yet mentioned how to arrange these materials. This is actually a very important issue. In fact, we often need to make a systematic display of the captured information. This may be from small to large (ascending) or from large to small (descending ). In this case, we can useORDERThis command is used to achieve our goal.
ORDERSyntax:
SELECT "column name"
FROM "table name"
[WHERE "condition"]
Order by "column name" [ASC, DESC];
[] RepresentativeWHEREYes. However, ifWHEREIf the clause exists, it is inORDERClause.ASCThe results are listed in ascending order, whileDESCThe results are listed in ascending order. If neither of them is written, we will useASC.
We can arrange the order based on several different positions. In this case,ORDERThe syntax of the clause is as follows (assuming there are two columns ):
Order by "column 1" [ASC, DESC], "column 2" [ASC, DESC]
If we select from small to large for both columns, then this clause will result in the result of "column 1" from small to large. If there are several pieces of data with the same value as "column 1", then these pieces of data are arranged from small to large based on "column 2.
For example, if we want to list the values in the Sales column from large to smallStore_InformationInformation in the table,
Store_InformationTable
Store_Name |
Sales |
Txn_Date |
Los Angeles |
1500 |
05-Jan 1999 |
San Diego |
250 |
07-Jan-1999 |
San Francisco |
300 |
08-Jan 1999 |
Boston |
700 |
08-Jan 1999 |
Let's get,
SELECT Store_Name, Sales, Txn_Date
FROM Store_Information
Order by Sales DESC;
Result:
Store_Name |
Sales |
Txn_Date |
Los Angeles |
1500 |
Jan-05-1999 |
Boston |
700 |
Jan-08-1999 |
San Francisco |
300 |
Jan-08-1999 |
San Diego |
250 |
Jan-07-1999 |
In the above example, we use the column name to specify the basis for the order. In addition to the column name, we can also use the column Order (based on the order in the SQL statement ). InSELECTThe first column is 1, the second column is 2, and so on. In the above example, we can use the following SQL statement to achieve the same effect:
SELECT Store_Name, Sales, Txn_Date
FROM Store_Information
Order by 2 DESC;
Linux has been tested as follows:
Bytes
Reprinted, please note: Xiao Liu