mysqlÖÐhavingµÄ×÷ÓÃ
having ×Ó¾äÓÃÓÚ¶Ô¾ÛºÏЧ¹û¾ÙÐÐɸѡ£¬Ïêϸ×÷ÓðüÀ¨£º¶Ô·Ö×éºóµÄÊý¾Ý¾ÙÐÐɸѡɨ³ý²»Öª×ãÌõ¼þµÄ×é»ùÓÚ¶à¸öÌõ¼þ¾ÙÐÐɸѡǶÌ×¾ÛºÏ
MySQL ÖÐ HAVING ×Ó¾äµÄ×÷ÓÃ
HAVING ×Ó¾äÓÃÓÚ¶Ô¾ÛºÏЧ¹û¾ÙÐÐɸѡ£¬ËüÔÚ GROUP BY ×Ó¾äÖ®ºóʹÓá£HAVING ×Ó¾äͨ¹ý¶Ô¾ÛºÏÁеÄÌõ¼þ¹ýÂË£¬½öÑ¡ÔñÖª×ã¸ÃÌõ¼þµÄ¾ÛºÏЧ¹ûÐС£
Ïêϸ×÷Óãº
¶Ô·Ö×éºóµÄÊý¾Ý¾ÙÐÐɸѡ£ºGROUP BY ·Ö×éºó£¬HAVING ×Ó¾ä¿ÉÒÔƾ֤¾ÛºÏº¯ÊýµÄÅÌËãЧ¹û£¬½øÒ»²½É¸Ñ¡³öÇкÏÌõ¼þµÄ×é¡£
ɨ³ý²»Öª×ãÌõ¼þµÄ×飺Ëü¿ÉÒÔɨ³ý¾ÛºÏЧ¹ûÖв»Öª×ãÖ¸¶¨Ìõ¼þµÄ×飬´Ó¶ø»ñµÃ¸üϸÁ£¶ÈµÄ¾ÛºÏЧ¹û¡£
»ùÓÚ¶à¸öÌõ¼þ¾ÙÐÐɸѡ£ºHAVING ×Ó¾ä¿ÉÒÔͬʱָ¶¨¶à¸öÌõ¼þ£¬ÓÃÓÚ¶Ô·Ö×éºóµÄÊý¾Ý¾ÙÐиüÖØ´óµÄɸѡ¡£
ǶÌ׾ۺϣºHAVING ×Ӿ仹¿ÉÒÔǶÌ×ÆäËû¾ÛºÏº¯Êý£¬ÒÔ½¨Éè¸üΪÖØ´óµÄɸѡÌõ¼þ¡£
ʹÓÃÓï·¨£º
SELECT ¾ÛºÏº¯Êý(ÁÐÃû) FROM ±íÃû GROUP BY ·Ö×éÁÐ HAVING ¾ÛºÏÌõ¼þ;
µÇ¼ºó¸´ÖÆ
ʾÀý£º
SELECT COUNT(*) AS ×ÜÊý FROM ¶©µ¥ GROUP BY ¿Í»§ID HAVING COUNT(*) > 1;
µÇ¼ºó¸´ÖÆ
¸ÃÅÌÎÊͳ¼Æÿ¸ö¿Í»§µÄ¶©µ¥ÊýÄ¿£¬²¢É¸Ñ¡³ö¶©µ¥ÊýÄ¿´óÓÚ 1 µÄ¿Í»§¡£
Óë WHERE ×Ó¾äµÄÇø±ð£º
WHERE ×Ó¾äÓÃÓÚÔÚ·Ö×éǰɸѡÊý¾Ý£¬¶ø HAVING ×Ó¾äÓÃÓÚÔÚ·Ö×éºóɸѡÊý¾Ý¡£
WHERE ×Ó¾ä¹ýÂ˵¥¸ö¼Í¼£¬¶ø HAVING ×Ó¾ä¹ýÂ˾ۺÏЧ¹û¡£
ÒÔÉϾÍÊÇmysqlÖÐhavingµÄ×÷ÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡