orderbyÔÚsqlÖеÄÓ÷¨
orderby×Ó¾äÓÃÓÚ¶ÔsqlÅÌÎÊЧ¹û°´Ö¸¶¨ÁÐÅÅÐò¡£Óï·¨£ºselect … order by column1 [asc|desc], column2 [asc|desc], …²ÎÊý£º1.ÒªÅÅÐòµÄÁÐ 2.¿ÉÑ¡µÄÅÅÐò˳Ðò£º[asc(ÉýÐò)|desc(½µÐò)]
OrderBy ÔÚ SQL ÖеÄÓ÷¨
OrderBy ×Ó¾äÓÃÓÚ¶Ô SQL ÅÌÎÊЧ¹ûƾָ֤¶¨µÄö¾ÙÐÐÅÅÐò¡£
Óï·¨£º
SELECT … ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], …
²ÎÊý£º
column1, column2, …£ºÒªÅÅÐòµÄÁÐ
ASC£ºÉýÐòÅÅÐò£¨´ÓСµ½´ó£©
DESC£º½µÐòÅÅÐò£¨´Ó´óµ½Ð¡£©
Ó÷¨£º
µ¥¸öÁÐÅÅÐò£º
SELECT name, age FROM customers ORDER BY name ASC;
µÇ¼ºó¸´ÖÆ
Õ⽫°´¿Í»§ÐÕÃûÉýÐòÅÅÐòЧ¹û¡£
¶à¸öÁÐÅÅÐò£º
SELECT name, age, city FROM customers ORDER BY name ASC, age DESC;
µÇ¼ºó¸´ÖÆ
Õ⽫Ê×ÏÈ°´ÐÕÃûÉýÐòÅÅÐò£¬È»ºó°´ÄêËê½µÐòÅÅÐò¡£
½µÐòÅÅÐò£º
SELECT name, age FROM customers ORDER BY age DESC;
µÇ¼ºó¸´ÖÆ
Õ⽫°´ÄêËê½µÐòÅÅÐòЧ¹û¡£
NULL Öµ´¦Öóͷ££º
ĬÈÏÇéÐÎÏ£¬NULL Öµ±»ÊÓΪ±È·Ç NULL ֵС¡£¿ÉÒÔʹÓà COALESCE() º¯Êý´¦Öóͷ£ NULL Öµ£¬ÀýÈ磺
SELECT name, age FROM customers ORDER BY COALESCE(age, 0) ASC;
µÇ¼ºó¸´ÖÆ
Õ⽫°´ÄêËêÉýÐòÅÅÐò£¬½« NULL ÖµÊÓΪ 0¡£
¸´ºÏÅÅÐò£º
¿ÉÒÔʹÓø´ºÏö¾ÙÐÐÅÅÐò£¬ÀýÈ磺
SELECT name, city, state FROM customers ORDER BY name ASC, city DESC;
µÇ¼ºó¸´ÖÆ
Õ⽫¶Ôÿ¸öÖÝÏÈ°´ÐÕÃûÉýÐòÅÅÐò£¬È»ºóÔÙ°´¶¼»á½µÐòÅÅÐò¡£
ÒÔÉϾÍÊÇorderbyÔÚsqlÖеÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡