×ðÁú¿­Ê±¹ÙÍøµÇ¼

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µÄ¼ÄÒåµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ