sqlÖÐgroupbyºÍorderbyÇø±ð
groupby ÓÃÓÚ¾ÛºÏÊý¾Ý£¬¶ø orderby ÓÃÓÚÅÅÐòÊý¾Ý¡£groupby ·µ»Ø×飬¶ø orderby ·µ»ØÅÅÐòºóµÄÐС£groupby ¿ÉÒÔ°üÀ¨¾ÛºÏº¯Êý£¬¶ø orderby ¿ÉÒÔ°üÀ¨Í¨ÀýÁС£
GroupBy Óë OrderBy ÔÚ SQL ÖеÄÇø±ð
GroupBy
½«¾ßÓÐÏàֵͬµÄÐÐ×éºÏµ½Ò»¸ö·Ö×éÖÐ
Ä¿µÄ£º»ã×Ü»ò¾ÛºÏÊý¾Ý
Óï·¨£º
SELECT column_name(s) FROM table_name GROUP BY column_name
µÇ¼ºó¸´ÖÆ
OrderBy
¶ÔЧ¹û¼¯°´Ö¸¶¨ÁеÄÖµ¾ÙÐÐÅÅÐò
Ä¿µÄ£º×éÖ¯ºÍÏÔʾÊý¾Ý
Óï·¨£º
SELECT column_name(s) FROM table_name ORDER BY column_name [ASC | DESC]
µÇ¼ºó¸´ÖÆ
Çø±ð
ÓÃ;£º GroupBy ÓÃÓÚ¾ÛºÏÊý¾Ý£¬¶ø OrderBy ÓÃÓÚÅÅÐòÊý¾Ý¡£
Ч¹û¼¯£º GroupBy ·µ»Ø×飬¶ø OrderBy ·µ»ØÅÅÐòºóµÄÐС£
ÁУº GroupBy ¿ÉÒÔ°üÀ¨¾ÛºÏº¯Êý£¬¶ø OrderBy ¿ÉÒÔ°üÀ¨Í¨ÀýÁС£
ʾÀý
¼ÙÉèÓÐÒ»ÕÅÃûΪ “Sales” µÄ±í£¬ÆäÖаüÀ¨ÒÔÏÂÊý¾Ý£º
Product | Sales |
---|---|
Apple | 100 |
Banana | 50 |
Apple | 75 |
Banana | 25 |
Orange | 120 |
GroupBy ʾÀý£º
SELECT Product, SUM(Sales) AS TotalSales FROM Sales GROUP BY Product;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
Product | TotalSales |
---|---|
Apple | 175 |
Banana | 75 |
Orange | 120 |
OrderBy ʾÀý£º
SELECT * FROM Sales ORDER BY Sales DESC;
µÇ¼ºó¸´ÖÆ
Ч¹û£¨°´ÏúÁ¿½µÐòÅÅÐò£©£º
Product | Sales |
---|---|
Orange | 120 |
Apple | 100 |
Apple | 75 |
Banana | 50 |
Banana | 25 |
ÒÔÉϾÍÊÇsqlÖÐgroupbyºÍorderbyÇø±ðµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡