mysqlÖÐgroupbyºÍhavingµÄ¹Øϵ
group by ·Ö×éÊý¾Ýºó£¬having ×Ó¾äÕë¶Ô·Ö×é¾ÙÐйýÂË£¬½ö±£´æÇкÏÌõ¼þµÄ·Ö×é¡£¶þÕß½áÊÊÓÃÓÚ·Ö×éÊý¾Ý¡¢¾ÛºÏÊý¾Ý¡¢¹ýÂË·Ö×é¡¢ÆÊÎöÊý¾Ý£¬²éÕÒģʽÇ÷ÊÆ£¬»ã×ÜÐÅÏ¢ºÍ½¨Éèͳ¼Æ±¨¸æ¡£
MySQL ÖÐ GROUP BY ºÍ HAVING ×Ó¾äµÄ¹Øϵ
GROUP BY ×Ó¾ä
½«ÏàÙÉÐзÖ×éµ½Ò»Æð£¬ÐγÉÒ»¸ö·Ö×éÜöÝÍ¡£
ÓÃÓÚ¶ÔÊý¾Ý¾ÙÐоۺϲÙ×÷£¬Èç SUM()¡¢COUNT()¡¢AVG() µÈ¡£
HAVING ×Ó¾äÖ»ÄÜÔÚ GROUP BY ×Ó¾äÖ®ºóʹÓá£
HAVING ×Ó¾ä
¶Ô·Ö×éºóµÄÊý¾Ý¾ÙÐйýÂË£¬½ö±£´æÇкÏÌض¨Ìõ¼þµÄ·Ö×é¡£
HAVING ×Ó¾ä±ØÐèÒýÓà GROUP BY ÖеķÖ×éÁлò¾ÛºÏº¯Êý¡£
Á½ÕßÖ®¼äµÄ¹Øϵ
GROUP BY ×Ó¾ä½ç˵·Ö×é·½·¨£¬¶ø HAVING ×Ó¾äÔòÖ¸¶¨·Ö×éºóµÄ¹ýÂËÌõ¼þ¡£ÕâÁ½¸ö×Ó¾äµÄ×éºÏÔÊÐí¶ÔÊý¾Ý¾ÙÐÐÒÔϲÙ×÷£º
·Ö×éÊý¾Ý
¶Ô·Ö×é¾ÙÐоۺÏ
¹ýÂ˲»ÇкÏÌõ¼þµÄ·Ö×é
ʾÀý
SELECT department_id, SUM(salary) FROM employee GROUP BY department_id HAVING SUM(salary) > 10000;
µÇ¼ºó¸´ÖÆ
¸ÃÅÌÎÊÊ×ÏȽ«Ô±¹¤°´²¿·Ö±àºÅ (department_id) ·Ö×é¡£
È»ºó£¬ËüÅÌËãÿ¸ö²¿·ÖµÄÈËΪ×ܺ͡£
×îºó£¬HAVING ×Ó¾ä½ö±£´æÈËΪ×ܺÍÁè¼Ý 10000 µÄ²¿·Ö¡£
ʹÓó¡¾°
GROUP BY ºÍ HAVING ×Ӿ侳£ÔÚÒÔÏÂÇéÐÎÏÂʹÓãº
ÆÊÎö¾ßÓжà¸öÊý¾Ý¼¯µÄÊý¾Ý
²éÕÒģʽºÍÇ÷ÊÆ
¹ýÂ˺ͻã×ÜÌض¨ÐÅÏ¢
½¨Éèͳ¼Æ±¨¸æ
ÒÔÉϾÍÊÇmysqlÖÐgroupbyºÍhavingµÄ¹ØϵµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡