sqlÖÐgroup by havingÓ÷¨
group by ºÍ having ×Ó¾äÓÃÓÚ¶Ô sql ÅÌÎÊЧ¹û¾ÙÐзÖ×éºÍ¹ýÂË¡£group by ½«Ðл®·ÖΪ×飬¶ø having ¹ýÂËÖª×ãÌض¨Ìõ¼þµÄ×é¡£
GROUP BY ºÍ HAVING ×Ó¾äÔÚ SQL ÖеÄÓ÷¨
СÐò£º
GROUP BY ºÍ HAVING ×Ó¾äÊÇ SQL ÖÐÓÃÓÚ¶ÔÅÌÎÊЧ¹û¾ÙÐзÖ×éºÍ¹ýÂ˵ĸ߼¶¾ÛºÏº¯Êý¡£
GROUP BY ×Ӿ䣺
GROUP BY ×Ó¾äÓÃÓÚ½«Ð§¹û¼¯ÖеÄÐл®·ÖΪ²î±ðµÄ×é¡£×éµÄ»®·Ö»ùÓÚÒ»¸ö»ò¶à¸öÁУ¬³ÆΪ·Ö×éÁС£Ã¿¸ö×éÖеÄËùÓÐÐж¼¹²ÏíÏàͬµÄ·Ö×éÁÐÖµ¡£
Óï·¨£º
SELECT aggregate_func(column_name) FROM table_name GROUP BY column_name1, column_name2, ...
µÇ¼ºó¸´ÖÆ
HAVING ×Ӿ䣺
HAVING ×Ó¾äÓÃÓÚ¹ýÂËÓÉ GROUP BY ×Ӿ䱬·¢µÄ×é¡£ËüֻѡÔñÖª×ãÌض¨Ìõ¼þµÄ×é¡£Ìõ¼þ¿ÉÒÔ»ùÓھۺϺ¯ÊýµÄЧ¹û¡£
Óï·¨£º
SELECT aggregate_func(column_name) FROM table_name GROUP BY column_name1, column_name2, ... HAVING condition
µÇ¼ºó¸´ÖÆ
ʹÓð¸Àý£º
²éÕÒÿ¸ö²úÆ·ÖÖ±ðÖÐÏúÊÛ×ܶîÁè¼ÝÌض¨ÖµµÄ²úÆ·
ÅÌËãÿ¸ö²¿·ÖµÄƽ¾ùÈËΪ£¬²¢¹ýÂ˳öƽ¾ùÈËΪ¸ßÓÚ¹«Ë¾Æ½¾ùÖµµÄ²¿·Ö
²éÕÒÿ¸ö¿Í»§¹ºÖÃÁËijһÌض¨ÃüÄ¿µÄÉÌÆ·
Çø±ð£º
GROUP BY ×Ó¾ä¶ÔÐоÙÐзÖ×飬¶ø HAVING ×Ó¾ä¶ÔÓÉ GROUP BY ×Ӿ䱬·¢µÄ×é¾ÙÐйýÂË¡£GROUP BY ×Ó¾ä±ØÐèÏÈÓÚ HAVING ×Ӿ䡣
ʾÀý£º
ÕÒ³öÏúÊÛ×ܶîÁè¼Ý 1000 ÃÀÔªµÄÿ¸ö²úÆ·ÖÖ±ð£º
SELECT product_category, SUM(sales) AS total_sales FROM sales_table GROUP BY product_category HAVING total_sales > 1000
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇsqlÖÐgroup by havingÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡