sql group by
sql group by Ó÷¨
2009-07-16 11:01:00 Òµ½ç | ÆÀÂÛ(0) | ä¯ÀÀ(1676)
group byÖ÷ÒªÊÇÓÃÀ´·Ö×éµÄ£¬Ôõô¸ö·Ö×éÄØ£¿
ÒÔÏÂÓÃÁ½¸öÀý×Ó˵Ã÷Á½¸öʹÓ÷½Ã棬1ÊǺÏÀíµÄ·µ»ØºÏ¼ÆÖµ£¨·ÀÖ¹µÑ¿¨¶û»ýÏÖÏ󣩣¬2ÊÇÓ÷Ö×éÀ´ÕÒ³öÖظ´µÄ¼Ç¼
====================================================================
¡ï¡ï¡ïÀý×Ó1£º¼ÙÈçÓÐÕâôһ¸ö±í£ºtab_1£¬ËüÓÐÁ½¸ö×ֶΣºxm¡¢gzlb¡¢je£¨ÐÕÃû¡¢¹¤×ÊÀà±ð¡¢½ð¶î£©£¬¾ßÌåÊý¾ÝÈçÏ£º
xm```````gzlb`````````je
-----------------------------------------
ÕÅÈý`````ÑøÀϽð`````1000
ÕÅÈý`````»¤Àí·Ñ`````200
ÕÅÈý`````ÆäËû```````50
.............
ÀîËÄ`````ÑøÀϽð`````800
ÀîËÄ`````ÆäËû```````50
.............
ÍõÎå`````ÑøÀϽð`````900
ÍõÎå`````ÆäËû```````35
¿ÉÒÔ¿´³öÿ¸öÈ˵Ť×ʶ¼ÊÇ°´Àà±ð´æ·ÅµÄ£¬Èç¹ûÒª·µ»ØºÏ¼ÆµÄÊý¾Ý£¬È磺
ÕÅÈý``````1250
ÀîËÄ``````850
ÍõÎå``````935
¸ÃÔõôдSQLÄØ£¿³õѧÕßÍùÍù»áÕâôд£ºselect xm, sum(je) from tab_1;µ«½á¹û»áÊÇʲôÑùÄØ£¿½á¹û½«ÊÇ£º
ÕÅÈý``````1250
ÕÅÈý``````850
ÕÅÈý``````935
ÀîËÄ``````1250
ÀîËÄ``````850
ÀîËÄ``````935
ÍõÎå``````1250
ÍõÎå``````850
ÍõÎå``````935
ÕâÊǽ«È«²¿×ֶζ¼½øÐÐÁËËùÓеÄÅÅÁÐ×éºÏ£¬¼´£ºµÑ¿¨¶û»ý£¡Òª·ÀÖ¹Õâ¸öÇé¿öµÄ·¢Éú£¬ÎÒÃǾͿÉÒÔÓõ½group by£¨·Ö×飩ÁË£¡
select xm, sum(je)
from tab_1
group by xm;
ÉÏÃæµÄSQLÖ¸¶¨ÁËÓÃxm×ֶηÖ×飬ÕâÑùÒ»À´¾Í·µ»Ø³öÕýÈ·µÄ½á¹ûÁË£º
ÕÅÈý``````1250
ÀîËÄ``````850
ÍõÎå``````935
====================================================================
¡ï¡ï¡ïÀý×Ó2£º¼ÙÉèÓÖÓÐÕâôһ¸ö±í£ºtab_2£¬ÓÐÕâЩ×ֶΣºbh, xm, dah,……£¨±àºÅ¡¢ÐÕÃû¡¢µµ°¸ºÅ¡¢……£©£¬
±ÈÈçÓÐÕâÑùµÄÇé¿ö£ºÏò¸Ã±í¼ÈëÊý¾ÝµÄÈËÔ±·Ç³£²»ÈÏÕ棬Öظ´Â¼ÈëÁ˲»ÉÙÊý¾Ý£¬È磺
bh``````xm```````dah
------------------------
1```````ÕÅÈý`````10001
2```````ÀîËÄ`````10002
3```````ÍõÎå`````10003
………………
84``````ÕÅÈý`````10001
85``````ÕÔÁù`````10004
………………
126`````ÍõÎå`````10003
¿ÉÒÔ¿´³öÕÅÈý¡¢ÍõÎå¸÷Öظ´ÁËÒ»´Î£¬¼ÙÉèÕâ¸ö±íÓм¸ÍòÌõÊý¾Ý£¬ÄÇôҪ²é³ö¾¿¾¹ÓжàÉÙÖظ´µÄ£¬¸ÃÔõô²éÄØ£¿
select bh, xm, dah
from tab_2
group by xm,
Ïà¹ØÎĵµ£º
Ô¶³ÌÁ¬½Ósql server 2000·þÎñÆ÷µÄ½â¾ö·½°¸
----ת×Ô£º·ÉºüСÎÝ
ÓÉÓ ......
.Frm644 { display:none; }
¡¡¡¡1¡£ÒѾ¼ìÑéµÄÓï¾äºÍÒÑÔÚ¹²Ïí³ØÖеÄÓï¾äÖ®¼äÒªÍêÈ«Ò»Ñù
¡¡¡¡2¡£±äÁ¿Ãû³Æ¾¡Á¿Ò»ÖÂ
¡¡¡¡3¡£ºÏÀíʹÓÃÍâÁª½Ó
¡¡¡¡4¡£ÉÙ
Óöà²ãǶÌ×
¡¡¡¡5¡£¶àÓò¢·¢
¡¡¡¡
¡¡¡¡Óï¾äµÄÓÅ»¯²½ÖèÒ»°ãÓУº
¡¡¡¡1¡£µ÷ÕûsgaÇø£¬Ê¹µÃsgaÇøµÄÊÇÓÃ×îÓÅ¡£
¡¡
¡¡2¡£sqlÓï¾ä±¾ÉíµÄÓÅ»¯£¬¹¤¾ßÓÐexplain,sql trace ......
Äãд¹ýÒ»ÌõsqlÓï¾äÀ´ÐÞ¸ÄÁ½¸ö±íµÄÊý¾ÝÂð£¿
UPDATE test.table1 t1,test.table2 t2 SET t1.aa='a',t1.bb='b',t2.cc='c',WHERE t1.u_id=t2.u_id AND t1.u_id='1' £»
table1µÄu_idºÍtable2µÄu_idÊÇÖ÷Íâ¼ü¹Øϵ ......
´Ó¹ÙÍøÏÂÔØSQL Server 2008µÄ180ÌìÊÔÓðæÆäʵÓëÕýʽ°æÄÚÈÝÊÇ»ù±¾ÏàͬµÄ£¬Î¨Ò»µÄÇø±ð¾ÍÔÚÓÚ°²×°ÅäÖÃÎļþÖÐËù°üº¬µÄkey¡£¸÷ÖÖ°æ±¾µÄSQL ServerÔÚ½øÐе½ÕâÒ»²½Ö®Ç°¶¼ÊÇÍêÈ«Ò»ÑùµÄ£º
Microsoft® SQL Server® 2008 Enterprise Evaluation£º¿ª·¢ÈËÔ±ÊÔÓÃÌåÑé
Microsoft® SQL Server® 2008 Enterprise Evaluatio ......