sqlÖÐʹÓþۺϺ¯Êý±ØÐè·Ö×éÂð
ÔÚ sql ÖÐʹÓþۺϺ¯Êýͨ³£ÐèÒª·Ö×飬ÒÔÈ·±£ÅÌËãµÄ׼ȷÐÔ¡£·Ö×é¿ÉÒÔƾ֤һ¸ö»ò¶à¸öö¾ÙÐУ¬½«Êý¾Ý»®·ÖΪ½ÏСµÄ×飬²¢ÔÚÿ¸ö×éÄÚÖ´ÐоۺÏÅÌËã¡£ÎÞ·Ö×é¾ÛºÏ¡¢µ¥ÁзÖ×éºÍ¶àÁзÖ×鶼ÊÇ¿ÉÐеÄÑ¡Ôñ£¬Ïêϸȡ¾öÓھۺϺ¯ÊýºÍ·Ö×éÐèÇó¡£
SQL ÖÐʹÓþۺϺ¯ÊýÊÇ·ñ±ØÐè·Ö×飿
ÃÕµ×£ºÍ¨³£ÇéÐÎÏÂÊÇ
ΪʲôÐèÒª·Ö×飿
¾ÛºÏº¯Êý£¨ÀýÈç SUM¡¢COUNT¡¢AVG µÈ£©¶ÔÒ»×éÊý¾ÝÖ´ÐÐÅÌËã²¢·µ»Øµ¥¸öЧ¹û¡£ÎªÁËÈ·±£ÅÌËãµÄ׼ȷÐÔ£¬±ØÐ轫Êý¾Ý·Ö×é¡£
·Ö×é¿ÉÒÔƾ֤һ¸ö»ò¶à¸öö¾ÙÐУ¬Ëü½«Êý¾Ý»®·ÖΪ½ÏСµÄ×飬²¢ÔÚÿ¸ö×éÄÚÖ´ÐоۺÏÅÌËã¡£
ʲôʱ¼ä²»ÐèÒª·Ö×飿
ÎÞ·Ö×é¾ÛºÏ£ºÄ³Ð©¾ÛºÏº¯Êý£¨ÀýÈç COUNT¡¢MIN¡¢MAX£©¿ÉÒÔÔÚ²»·Ö×éµÄÇéÐÎ϶ÔÕû¸öÊý¾Ý¼¯¾ÙÐÐÅÌËã¡£
µ¥ÁзÖ×飺µ±¾ÛºÏº¯Êý½öƾ֤µ¥¸öÁзÖ×éʱ£¬¿ÉÒÔʹÓà GROUP BY ×Ӿ䡣
¶àÁзÖ×飺ÈôÊǾۺϺ¯ÊýÐèҪƾ֤¶à¸öÁзÖ×飬ÔòÐèҪʹÓà GROUP BY ALL ×Ӿ䡣
ʾÀý
-- ÎÞ·Ö×é¾ÛºÏ£ºÅÌËãËùÓÐÐеÄ×ÜºÍ SELECT SUM(salary) FROM employees; -- µ¥ÁзÖ×飺ÅÌËãÿ¸ö²¿·ÖµÄÔ±¹¤ÈËÊý SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department; -- ¶àÁзÖ×飺ÅÌËãÿ¸ö²¿·ÖºÍְλµÄƽ¾ùÈËΪ SELECT department, job_title, AVG(salary) AS average_salary FROM employees GROUP BY ALL department, job_title;
µÇ¼ºó¸´ÖÆ
½áÂÛ
ÔÚ SQL ÖÐʹÓþۺϺ¯Êýʱ£¬Í¨³£ÐèҪƾ֤һ¸ö»ò¶à¸öÁзÖ×éÒÔÈ·±£ÅÌËãµÄ׼ȷÐÔ¡£¿ÉÊÇ£¬ÔÚijЩÇéÐÎÏ£¬ÎÞ·Ö×é¾ÛºÏ»òµ¥ÁзÖ×éÊÇ¿ÉÄܵġ£
ÒÔÉϾÍÊÇsqlÖÐʹÓþۺϺ¯Êý±ØÐè·Ö×éÂðµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡