sqlÖÐorder byµÄ¼ÄÒå
sqlÖÐµÄ order by ×Ó¾äÓÃÓÚ¶ÔÅÌÎÊЧ¹û°´Ö¸¶¨Ã¶¾ÙÐÐÅÅÐò£¬¿ÉÖ¸¶¨ÉýÐò£¨asc£©»ò½µÐò£¨desc£©ÅÅÁУ¬»¹¿ÉÖ¸¶¨¶à¸öÅÅÐòÌõ¼þ¡£Ä¬ÈÏÇéÐÎÏ£¬null ÖµÊÓΪ×î´óÖµ£¬¿ÉʹÓà is null ×Ó¾ä¸ü¸Ä´ËÐÐΪ¡£
SQL ÖÐ ORDER BY µÄ¼ÄÒå
ORDER BY ×Ó¾äÓÃÓÚ¶ÔÅÌÎÊЧ¹ûƾָ֤¶¨Ã¶¾ÙÐÐÅÅÐò¡£Ëü¿ÉÒÔÈÃÄú°´ÉýÐò£¨ÉýÐò£©»ò½µÐò£¨½µÐò£©ÅÅÁÐÊý¾Ý¡£
Óï·¨£º
SELECT * FROM table_name ORDER BY column_name [ASC | DESC];
µÇ¼ºó¸´ÖÆ
column_name£ºÒªÆ¾Ö¤ÆäÅÅÐòµÄÁС£
ASC£ºÖ¸¶¨ÉýÐòÅÅÐò£¨´Ó×îСµ½×î´ó£©¡£
DESC£ºÖ¸¶¨½µÐòÅÅÐò£¨´Ó×î´óµ½×îС£©¡£
ʾÀý£º
Òª°´ employees ±íÖÐµÄ “salary” ÁÐÉýÐòÅÅÐò£¬¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT * FROM employees ORDER BY salary ASC;
µÇ¼ºó¸´ÖÆ
Òª°´ “name” ÁнµÐòÅÅÐò£¬¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT * FROM employees ORDER BY name DESC;
µÇ¼ºó¸´ÖÆ
¶à¸öÅÅÐòÌõ¼þ£º
ORDER BY ×Ó¾ä¿ÉÒÔÖ¸¶¨¶à¸öÅÅÐòÌõ¼þ¡£ÕâЩÌõ¼þ°´Ö¸¶¨Ë³ÐòÓ¦Óá£ÀýÈ磬Ҫ°´ “salary” ÁнµÐò£¬È»ºó°´ “name” ÁÐÉýÐòÅÅÐò£¬¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT * FROM employees ORDER BY salary DESC, name ASC;
µÇ¼ºó¸´ÖÆ
NULL ÖµµÄÅÅÐò£º
ĬÈÏÇéÐÎÏ£¬NULL ÖµÔÚÅÅÐòÖб»ÊÓΪ×î´óÖµ¡£Òª¸ü¸Ä´ËÐÐΪ£¬¿ÉÒÔʹÓà IS NULL ×Ӿ䣺
SELECT * FROM employees ORDER BY salary IS NULL DESC, salary ASC;
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬NULL Öµ½«±»ÊÓΪ×îСֵ²¢°´½µÐòÅÅÐò¡£
ÒÔÉϾÍÊÇsqlÖÐorder byµÄ¼ÄÒåµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡