sqlÖÐorder byµÄ×÷ÓÃ
sql ÖÐ order by ×Ó¾ä¶ÔЧ¹û¼¯ÖеÄÐоÙÐÐÅÅÐò£¬ÒÔ°´Ìض¨Ë³ÐòÏÔʾÊý¾Ý¡£Ëü°´ÉýÐò»ò½µÐò¶ÔÒ»¸ö»ò¶à¸öö¾ÙÐÐÅÅÐò£¬²¢Ö§³Ö¶àÁÐÅÅÐò¡¢¿ÕÖµ´¦ÀíµÈ¸ß¼¶Ó÷¨¡£
SQL ÖÐ ORDER BY µÄ×÷ÓÃ
ÔÚ SQL ÖУ¬ORDER BY ×Ó¾äÓÃÓÚ¶ÔЧ¹û¼¯ÖеÄÐоÙÐÐÅÅÐò¡£ËüÔÊÐíÄúƾ֤һ¸ö»ò¶à¸öÁа´ÉýÐò»ò½µÐòÅÅÁÐÊý¾Ý¡£
Ö÷Òª×÷Óãº
ÅÅÁÐЧ¹û¼¯£º¶Ô´ÓÅÌÎÊÖзµ»ØµÄÐоÙÐÐÅÅÐò£¬´Ó¶ø°´Ìض¨Ë³ÐòÏÔʾÊý¾Ý¡£
°´Ìض¨ÁзÖ×飺½«ÐзÖ×éµ½¾ßÓÐÏàֵͬµÄÒ»×éÁÐÖУ¬È»ºóÔÙ¶Ôÿһ×é¾ÙÐÐÅÅÐò¡£
Óï·¨£º
SELECT ... FROM ... WHERE ... ORDER BY column_name1 [ASC | DESC], column_name2 [ASC | DESC], ...;
µÇ¼ºó¸´ÖÆ
column_name£ºÒªÅÅÐòµÄÁеÄÃû³Æ¡£
ASC£º°´ÉýÐòÅÅÐò£¨´Ó×îСµ½×î´ó£©¡£
DESC£º°´½µÐòÅÅÐò£¨´Ó×î´óµ½×îС£©¡£
ʾÀý£º
ÈÃÎÒÃÇ˼Á¿Ò»¸ö°üÀ¨Ö°Ô±ÐÅÏ¢µÄ±í£º
CREATE TABLE persons ( id INT PRIMARY KEY, name VARCHAR(255), age INT );
µÇ¼ºó¸´ÖÆ
Òª°´ÐÕÃûÉýÐòÅÅÁбíÖеÄÖ°Ô±£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT * FROM persons ORDER BY name ASC;
µÇ¼ºó¸´ÖÆ
Õâ»á·µ»Ø°´ÐÕÃû´Ó A µ½ Z ÅÅÐòµÄÐС£
¸ß¼¶Ó÷¨£º
ORDER BY ×Ӿ仹֧³ÖһЩ¸ß¼¶Ó÷¨£º
¶àÁÐÅÅÐò£ºÒ»´Î¶Ô¶à¸öö¾ÙÐÐÅÅÐò£¬ÀýÈç°´ÐÕÃûÉýÐò£¬ÔÙ°´ÄêËê½µÐò¡£
¿ÕÖµ´¦Àí£ºÖ¸¶¨¿ÕÖµÊÇÅÅÔÚµÚһλÕÕ¾É×îºóһλ¡£
NULLS FIRST/LAST£º½« NULL ÖµÅÅÔÚЧ¹û¼¯µÄ×îÇ°Ãæ»ò×îºóÃæ¡£
ͨ¹ýʹÓà ORDER BY ×Ӿ䣬Äú¿ÉÒÔ¸ßЧµØ×éÖ¯ºÍÅÅÁÐÊý¾Ý£¬´Ó¶ø¼ò»¯ÅÌÎʺͱ¨¸æµÄÌìÉú¡£
ÒÔÉϾÍÊÇsqlÖÐorder byµÄ×÷ÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡