mysqlÖÐhavingʲôÒâ˼
having ×Ó¾äÓÃÓÚɸѡ·Ö×éºóµÄЧ¹û¼¯£¬Ó¦ÓÃÓÚ»ã×ܺóµÄÊý¾Ý¶ø·ÇÔʼÊý¾Ý¡£Ëü¿ÉÒÔÑïÆú²»Öª×ãÌõ¼þµÄÐС¢Ï¸»¯Ð§¹û¼¯ºÍÌáÈ¡Ìض¨ÐÅÏ¢¡£ÀýÈ磬´ËÅÌÎÊÕÒ³öÏúÊÛ¶î´óÓÚ 100 ÃÀÔªµÄ¶©µ¥£ºselect customer_id, sum(amount) as total_amount from orders group by customer_id having total_amount > 100;
ÔÚ MySQL ÖУ¬HAVING µÄ¼ÄÒå
HAVING ×Ó¾äÓÃÓÚ¶Ô GROUP BY ·Ö×éºóµÄЧ¹û¼¯¾ÙÐнøÒ»²½µÄ¹ýÂË¡£ËüÀàËÆÓÚ WHERE ×Ӿ䣬¿ÉÊÇÓ¦ÓÃÓÚ»ã×ܺóµÄÊý¾Ý£¬¶ø²»ÊÇÔʼÊý¾Ý¡£
ʹÓó¡¾°
HAVING ×Ó¾äͨ³£ÓÃÓÚ´Ó·Ö×éÊý¾ÝÖÐɸѡ³öÖª×ãÌض¨Ìõ¼þµÄÐС£ÀýÈ磺
ÕÒ³öÏúÊÛ¶î´óÓÚ 100 ÃÀÔªµÄ¶©µ¥¡£
ÕÒ³öÿ¸ö¿Í»§¹ºÖõÄÉÌÆ·ÊýÄ¿´óÓÚ 5 ¸öµÄ¶©µ¥¡£
Óï·¨
HAVING ×Ó¾äµÄÓï·¨ÈçÏ£º
HAVING
µÇ¼ºó¸´ÖÆ
ÆäÖÐÊÇÈκÎÓÐÓÃµÄ SQL ±í´ïʽ£¬¿ÉÒÔ°üÀ¨¾ÛºÏº¯Êý£¨Èç SUM¡¢COUNT µÈ£©ºÍ½ÏÁ¿ÔËËã·û£¨Èç =¡¢>¡¢
×÷ÓÃ
HAVING ×Ó¾äͨ¹ýɸѡ·Ö×éºóµÄÊý¾Ý£¬¿ÉÒÔÌṩÒÔϹ¦Ð§£º
ÑïÆú²»Öª×ãÌõ¼þµÄÐÐ
½øÒ»²½Ï¸»¯Ð§¹û¼¯
ÌáÈ¡Ìض¨ÐÅÏ¢
ʾÀý
ÒÔÏÂʾÀýÅÌÎÊÕÒ³öÏúÊÛ¶î´óÓÚ 100 ÃÀÔªµÄ¶©µ¥£º
SELECT customer_id, SUM(amount) AS total_amount FROM orders GROUP BY customer_id HAVING total_amount > 100;
µÇ¼ºó¸´ÖÆ
¸ÃÅÌÎʻ᷵»ØÒÔÏÂЧ¹û£º
customer_id | total_amount |
---|---|
1 | 120.50 |
2 | 150.75 |
3 | 115.20 |
ÒÔÉϾÍÊÇmysqlÖÐhavingʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡