ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

MySQL¶i¶¥_SELECT½g(¤T)

MySQL¶i¶¥_SELECT½g(¤T)

not¡G§_©w¥ô¦óÅÞ¿èªí¹F¦¡¡A©Î¬OÃöÁäµü¡A¦plike¡Anull¡Abetweenµ¥¡C
group by©Mhaving¤l¥y¦bselect»y¥y¤¤¨Ï¥Î¡A¥i¥H±Nªí¹º¤À¦¨²Õ¨Ãªð¦^¤Ç°thaving¤l¥y±ø¥óªº²Õ¡C
»yªk¡Gselect»y¥y¶}ÀY
     group by [all] aggregate_free_expression [,aggregate_free_expression]*
     [having search_conditions]
     select»y¥yµ²§À
group by¡G«ü©wªí±N¹º¤Àªº²Õ¸s¡A¦pªG¦bselectªí¶µ¤¤¥]§t¶°¦X¨ç¼Æ¡A«h¬°¦U²Õ­pºâ¤@­ÓÁ`­p­È¡C³o¨ÇÁ`­p­Èªºµ²ªG¥H·sªº¦CÅã¥Ü¡A¦Ó¤£¬O·sªº¦æ¡C¦b having¤l¥y¤¤¨Ï¥ÎªÌ¥i¥H¤Þ¥Î³o¨Ç·sªºÁ`­p¦C¡C¦bgroup by¤§«eªºselect_list¤¤¥i¥H¨Ï¥Îavg¡Bcount¡Bmax¡Bmin©Msumµ¥¶°¦X¨ç¼Æ¡Cªí¥i¥H³Q¥ô·N¦Cªº²Õ¦X¤À²Õ¡C
all¡G¦bµ²ªG¤¤¥]§t©Ò¦³²Õ¸sªºTransact-SQLÂX®i¡A³o¸Ìªº©Ò¦³²Õ¸s¬Æ¦Ü¥]¬A¨º¨Ç³Qwhere¤l¥y©Ò±Æ°£ªº²Õ¸s¡C¦pªG¦P®É¨Ï¥Îhaving¤l¥y¡A±N¹ïallªº·N¸q¶i¦æ§_©w¡C
aggregate_free_expression¡G¤£¥]§t¶°¦X¨ç¼Æªºªí¹F¦¡¡ATransact-SQLÂX®i¤¹³\¦b¥Î¦C¦WºÙ¤À²Õªº¦P®É¡A¥ÎµL¶°¦X¨ç¼Æªºªí¹F¦¡¤À²Õ¡C
having¡G¬°group by¤l¥y³]¸m±ø¥ó¡AÃþ¦ü©ówhere¬°select»y¥y³]¸m±ø¥óªº¤èªk¡Chavingªº¬d§ä±ø¥ó¥i¥H¥]¬A¶°¦X¨ç¼Æªí¹F¦¡¡C°£¦¹¤§¥~¡A¥¦ªº¬d§ä±ø¥ó»Pwhere¬d§ä±ø¥ó¬Û¦P¡C
order by¡G«ö¦C±Æ¦Cµ²ªG¡C¹ïselect¿é¥Xªº¦C¥i¥H¥Î¦C¦W¡B¦C§O¦W©Î¦C¦ì¸m¨Ó¤Þ¥Î¡C¨Ò¦p¡Gselect id as myid,name as myname from mytable group by id¡Bselect id as myid,name as myname from mytable group by myid¡Bselect id as myid,name as myname from mytable group by 1³o¤T¥y¬O§¹¥þµ¥»ùªº¡C·íµM¡A§Ú­Ì¤£ÃÙ¦¨¥Î²Ä¤TºØ¥Îªk¡A³o±Nµ¹µ{¦¡ªº¥iŪ©Ê±a¨Ó¤£¦nªº¼vÅT¡C ¬°¤F¥H­°§Ç±Æ¦C¡A§âDESCÃöÁäµü¥[¨ìorder by¤l¥y¤¤§A­n±Æ§Çªº¦C¦W«e¡C¯Ê¬Ù¬O¤É§Ç¡A§A¤]¥i¥H¥ÎASCÃöÁäµü©ú½T«ü©w¡C
limit ¤l¥y¡G¥Î¨Ó­­¨îselect»y¥yªð¦^ªº¦æ¼Æ¡Climit¨ú1­Ó©Î2­Ó¼Æ¦r°Ñ¼Æ¡A¦pªGµ¹©w2­Ó°Ñ¼Æ¡A²Ä¤@­Ó«ü©w­nªð¦^ªº²Ä¤@¦æªº°¾²¾¶q¡A²Ä¤G­Ó«ü©wªð¦^¦æªº³Ì¤j¼Æ¥Ø¡Cªì©l¦æªº°¾²¾¶q¬O0¡]¤£¬O1¡^¡C¦pªGµ¹©w¤@­Ó°Ñ¼Æ¡A¥¦«ü¥X°¾²¾¶q¬°0ªºªð¦^¦æªº³Ì¤j¼Æ¥Ø¡C¤]´N¬O»¡limit 5©Mlimit 0,5§¹¥þµ¥»ù¡C
¦Ü©óprocedureÃöÁäµüªº§t¸q¡A§Ú¤]¨S·d±o¤Ó²M·¡¡A¦n¹³¬O¹ïÀx¦s¹Lµ{ªº¤ä«ù¡A¦ÓMySQL¥»¨­¤£¤ä«ùÀx¦s¹Lµ{¡A¬Ý¨Ó¬O¬°¤F±N¨ÓÂX¥Rªº»Ý­n¦Ó«O¯dªº§a
­Ó¤Hñ¦W@

        ÀR«ä¦Û¦b : ¡i®É¤é²öªÅ¹L¡j¤@­Ó¤H¦b¥@¶¡°µ¤F¦h¤Ö¨Æ¡A´Nµ¥©ó¹Ø©R¦³¦hªø¡C¦]¦¹¥²¶·»P®É¶¡Ävª§¡A¤Á²ö¨Ï®É¤éªÅ¹L¡C
ªð¦^¦Cªí ¤W¤@¥DÃD