sqlÖеÄhavingÊÇʲôÒâ˼
sql ÖÐµÄ having ×Ó¾äÓÃÓÚÔÚ¾ÛºÏÅÌÎÊÖÐɸѡ¾ÛºÏЧ¹û¡£ËüÔÚ¶ÔÊý¾Ý¾ÙÐзÖ×é²¢ÅÌËã¾ÛºÏÖµºóÓ¦Óã¬Æ¾Ö¤¾ÛºÏЧ¹û¹ýÂËÐУ¬Óë where ×Ó¾äÓÃÓÚÔÚ¾ÛºÏ֮ǰ¹ýÂËÔʼÊý¾Ý²î±ð¡£having ×Ó¾ä¿ÉÓÃÓÚƾ֤¾ÛºÏº¯ÊýµÄЧ¹û¶ÔÊý¾Ý¾ÙÐÐÎÞаɸѡ£¬µ«Ö»ÄÜÓÃÓÚ¾ÛºÏÅÌÎÊ£¬ÇҾۺϺ¯ÊýµÄÁбØÐèÔÚ group by ×Ó¾äÖÐʹÓá£
SQLÖеÄHAVING×Ó¾ä
HAVING×Ó¾äÓÃÓÚÔÚSQL¾ÛºÏÅÌÎÊÖжԾۺÏЧ¹û¾ÙÐÐɸѡ¡£ËüÀàËÆÓÚWHERE×Ӿ䣬µ«HAVING×Ó¾äÊÇÔÚ¶ÔÊý¾Ý¾ÙÐзÖ×é²¢ÅÌËã¾ÛºÏÖµÖ®ºóÓ¦Óõġ£
Óï·¨£º
SELECT ¾ÛºÏº¯Êý(ÁÐÃû) FROM ±íÃû GROUP BY ·Ö×éÁÐ HAVING ¾ÛºÏÌõ¼þ;
µÇ¼ºó¸´ÖÆ
ÓÃ;£º
HAVING×Ó¾äÓÃÓÚÔÚÅÌËã³ö¾ÛºÏÖµºó¹ýÂË·Ö×éЧ¹û¡£Ëü¿ÉÒÔƾ֤¾ÛºÏЧ¹û£¬É¸Ñ¡³öÖª×ãÌض¨Ìõ¼þµÄÐС£
ÓëWHERE×Ó¾äµÄÇø±ð£º
WHERE×Ó¾äÓÃÓÚÔÚ¾ÛºÏ֮ǰ¹ýÂËÔʼÊý¾Ý£¬¶øHAVING×Ó¾äÓÃÓÚÔÚ¾ÛºÏÖ®ºó¹ýÂ˾ۺÏЧ¹û¡£
ʾÀý£º
Òª²éÕÒÏúÊÛ¶î´óÓÚ1000ÃÀÔªµÄ¶©µ¥£¬¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT SUM(amount) AS total_sales FROM orders GROUP BY customer_id HAVING total_sales > 1000;
µÇ¼ºó¸´ÖÆ
Óŵ㣺
ÔÊÐíÔÚ¾ÛºÏЧ¹ûµÄ»ù´¡ÉϹýÂËÊý¾Ý¡£
ÌṩÁ˸ü¸ßµÄÎÞаÐÔ£¬¿ÉÒÔƾ֤¾ÛºÏº¯ÊýµÄЧ¹û¶ÔÊý¾Ý¾ÙÐÐɸѡ¡£
×¢ÖØÊÂÏ
HAVING×Ó¾äÖ»ÄÜÓÃÓÚ¾ÛºÏÅÌÎÊ¡£
¾ÛºÏº¯ÊýµÄÁбØÐèÔÚGROUP BY×Ó¾äÖÐʹÓã¬ÒÔ±ã¶ÔЧ¹û¾ÙÐзÖ×é¡£
ÒÔÉϾÍÊÇsqlÖеÄhavingÊÇʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡