How to do SQL sorting

The order by syntax is as follows:

SELECT "field name"
FROM "table name"
[WHERE "condition"]
ORDER BY "field name" [ASC, DESC];

[ ] means where is required. However, if the where clause exists, it is before the order by clause , the asc table results will be listed in descending order, and the desc table results will be listed in descending order. If neither is written, then we use asc.

1 Several sorts of writing:

Single-column ascending order: select name from student order by name; (default ascending order, even if asc is not written)
Single-column descending order: select name from student order by name desc;
multi-column ascending order: select id, name from student order by id, name;
multiple columns Mixed sorting: select id, name from student order by id desc, name asc; (first descending by id, ascending by name if the id is the same)

 

2.1 select * from article where nameid=1 order by arid; //find the article table (article), sort by user ID (nameid) order by article ID (rid) default ascending order (asc);

 

2.2 If you need to see from the back, use descending order (desc),

select  * from article where nameid=1 order by arid desc;  

 

2.3 select * from table where a like '%b%' order by 字段 desc/asc;   //

 

2.4 Put null first: select * from student order by name asc nulls first;

Put null at the end: select * from student order by name desc nulls last;

 

2.5 Force a column to be first

select * from student order by decode(id, 3, 1, 2), id; // the value whose id is 3 ranks first

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324992863&siteId=291194637