{"id":1110,"date":"2010-06-01T22:28:00","date_gmt":"2010-06-01T14:28:00","guid":{"rendered":"\/?p=1110"},"modified":"2010-06-01T22:28:00","modified_gmt":"2010-06-01T22:28:00","slug":"1110","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/mysql\/1110.html","title":{"rendered":"mysql\u4e2dGROUP BY\u7ed3\u5408GROUP_CONCAT\u7684\u4f7f\u7528"},"content":{"rendered":"

\u6211\u4eec\u77e5\u9053\uff0cgroup by\u53ef\u4ee5\u5c06sql\u67e5\u8be2\u7ed3\u679c\u6309\u7167group by\u540e\u9762\u5217\u8fdb\u884c\u5206\u7c7b\u663e\u793a\u3002\u6bd4\u5982\uff1a<\/p>\n

\n
    \n
  1. select<\/span> columnA,columnB <\/span>from<\/span> <\/span>table<\/span> <\/span>group<\/span> <\/span>by<\/span> columnA,columnB  <\/span><\/span><\/li>\n<\/ol>\n<\/div>\n

          \u5219\u67e5\u8be2\u7ed3\u679c\u5c06\u6309\u7167columnA\u548ccolumnB\u5206\u7c7b\u663e\u793a\u3002\u6ca1\u6709\u663e\u793a\u5728group by\u4e2d\u7684\u5217\u4e0d\u80fd\u76f4\u63a5\u4f5c\u4e3a\u8fd4\u56de\u5217\u653e\u5728sql\u8bed\u53e5\u4e2d\uff0c\u6bd4\u5982\u5982\u4e0bsql\u5c31\u662f\u4e0d\u6b63\u786e\u7684<\/p>\n

    \n
      \n
    1. select<\/span> columnA,columnC <\/span>from<\/span> <\/span>table<\/span> <\/span>group<\/span> <\/span>by<\/span> columnA  <\/span><\/span><\/li>\n<\/ol>\n<\/div>\n

           \u7531\u4e8ecolumnC\u4e0d\u5728group by\u7684\u8303\u56f4\u4e4b\u7c7b\uff0c\u6240\u4ee5\u8fd9\u6837\u5199\u662f\u4e0d\u5bf9\u7684\uff0c\u6240\u5e78\u7684\u662f\uff0cgroup by\u652f\u6301\u4e00\u4e9bsql \u51fd\u6570\u7684\u4f7f\u7528\uff0c\u6bd4\u5982SUM,AVG,COUNT\u7b49\u7b49\u3002\u8fd9\u4e9b\u90fd\u6bd4\u8f83\u5e38\u7528\uff0c\u4eca\u5929\u6211\u8981\u8bb0\u5f55\u4e0b\u7684\u662f\u8fd9\u4e2a\u4e0d\u5e38\u7528\u7684GROUP_CONCAT\u3002<\/p>\n

             \u6709\u4e00\u4e2a\u9700\u6c42\uff0c\u9700\u8981\u7528\u5230group by \u624d\u80fd\u5b9e\u73b0\uff0c\u53ef\u662f\uff0c\u6211\u540c\u662f\u8fd8\u9700\u8981\u8fd4\u56de\u67d0\u5217\u7684\u6240\u6709\u7ed3\u679c\uff0c\uff08\u6ce8\u610f\uff0c\u4e0d\u662f\u505aavg\uff0csum\u7b49\u64cd\u4f5c\uff0c\u6211\u8981\u679a\u4e3e\u8fd9\u5217\u7684\u6240\u6709\u7ed3\u679c\uff09\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u7528\u5230 GROUP_CONCAT\u3002<\/p>\n

      \u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

      \u6211\u6709\u4e00\u5f20\u6570\u636e\u5e93\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n\n\n\n
      \u5217\u540d<\/td>\n\u542b\u4e49<\/td>\n<\/tr>\n
      year<\/td>\n\u5e74\u4efd<\/td>\n<\/tr>\n
      month<\/td>\n\u6708\u4efd<\/td>\n<\/tr>\n
      volumn<\/td>\n\u671f\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

      \u8be5\u8868\u5b58\u50a8\u4e86\u67d0\u6742\u5fd7\u7684\u5e74\u4efd\uff0c\u6708\u4efd\u548c\u671f\u6570\u3002\u5982\u679c\u9700\u6c42\u5bf9\u8be5\u8868\u5185\u5bb9\u4f5c\u5982\u4e0b\u663e\u793a\uff1a<\/p>\n\n\n\n\n\n\n\n
      2010\u5e7412\u6708<\/td>\n\u7b2c1\u671f\u7b2c2\u671f\u7b2c3\u671f \u7b2c4\u671f<\/td>\n<\/tr>\n
      2010\u5e7411\u6708<\/td>\n\u7b2c1\u671f\u7b2c2\u671f\u7b2c3\u671f \u7b2c4\u671f\u7b2c5\u671f<\/td>\n<\/tr>\n
      2010\u5e7410\u6708<\/td>\n\u7b2c1\u671f\u7b2c2\u671f\u7b2c3\u671f \u7b2c4\u671f<\/td>\n<\/tr>\n
      2010\u5e749\u6708<\/td>\n\u7b2c1\u671f\u7b2c2\u671f\u7b2c3\u671f \u7b2c4\u671f\u7b2c5\u671f<\/td>\n<\/tr>\n
      2010\u5e748\u6708<\/td>\n\u7b2c1\u671f\u7b2c2\u671f\u7b2c3\u671f \u7b2c4\u671f <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

      sql\u8be5\u600e\u4e48\u5199\u5462\uff1f\u6309\u7167\u5e74\u4efd\u548c\u6708\u4efd\u505agroup by\uff1f\u7136\u540e\u6309\u7167\u5e74\u4efd\u548c\u6708\u4efd\u505a\u5012\u53d9\u6392\u5217\uff1f<\/p>\n

      \n
        \n
      1. select<\/span> <\/span>year<\/span>,<\/span>month<\/span> <\/span>from<\/span> magazine <\/span>group<\/span> <\/span>by<\/span> <\/span>year<\/span>,<\/span>month<\/span> <\/span>order<\/span> <\/span>by<\/span> <\/span>year<\/span> <\/span>desc<\/span>,<\/span>month<\/span> <\/span>desc<\/span>  <\/span><\/span><\/li>\n<\/ol>\n<\/div>\n

          <\/font> \u90a3\u5177\u4f53\u7684\u671f\u6570\u4fe1\u606f\u5c31\u4e22\u4e86\uff1f\u80fd\u4e0d\u80fd\u505agroup by\u7684\u65f6\u5019\uff0c\u8fd8\u80fd\u8fd4\u56de\u5728\u67d0\u4e2a\u5e74\u4efdyear\u548c\u6708\u4efdmonth\u5206\u7ec4\u4e0b\u7684\u6240\u6709\u671f\u6570volumn\u4fe1\u606f\uff1f\uff08\u67d0\u4e2a\u5e74\u4efd+\u6708\u4efd\u4e0b\u7684\u671f\u6570\u4fe1\u606f\u662f\u4e0d\u56fa\u5b9a\u7684\uff0c\u53ea\u80fd\u901a\u8fc7\u6570\u636e\u5e93\u67e5\u8be2\u624d\u80fd\u83b7\u5f97\uff09<\/p>\n

        \u8be5\u662fGROUP_CONCAT\u4e0a\u9635\u7684\u65f6\u5019\u4e86\u3002<\/p>\n

        \n
          \n
        1. select<\/span> <\/span>year<\/span>,<\/span>month<\/span> GROUP_CONCAT(volumn) <\/span>from<\/span> magazine <\/span>group<\/span> <\/span>by<\/span> <\/span>year<\/span>,<\/span>month<\/span> <\/span>order<\/span> <\/span>by<\/span> <\/span>year<\/span> <\/span>desc<\/span>, <\/span>month<\/span> <\/span>desc<\/span>  <\/span><\/span><\/li>\n<\/ol>\n<\/div>\n

          \u8fd9\u6837\uff0c\u67e5\u8be2\u7684\u8fd4\u56de\u7ed3\u679c\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n\n\n\n\n\n
          year<\/td>\nmonth<\/td>\nGROUP_CONCAT(volumn)<\/td>\n<\/tr>\n
          2010<\/td>\n12<\/td>\n1,2,3,4<\/td>\n<\/tr>\n
          2010<\/td>\n11<\/td>\n1,2,3,4,5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

          \u4e0d\u9519\u5427\uff1f<\/p>\n

                \u8fd8\u6709\u70b9\u95ee\u9898\u9700\u8981\u8865\u5145\u4e0b\uff0c\u5c31\u662f\u4f5c\u4e3aGROUP_CONCAT\u51fd\u6570\u53c2\u6570\u7684\u5b57\u6bb5\uff0c\u5982\u8fc7\u8fd4\u56de\u503c\u4e3astring\uff0c\u5219\u4e0a\u9762\u7684sql\u8bed\u53e5\u5df2\u7ecf\u6ca1\u6709\u95ee\u9898\uff0c\u4f46\u662f\u5982\u679c\u662f number\uff0c\u5219\u8fd4\u56de\u7684GROUP_CONCAT(volumn)\u503c\u4e3aBLOB\u7c7b\u578b\uff08\u5176\u5b9e\u4e0a\u9762\u4f8b\u5b50\u8fd4\u56de\u7684\u5c31\u662f\u4e00\u4e2ablob\u7c7b\u578b\uff0c\u6211\u53ea\u662f\u4e3a\u4e86\u6f14\u793a\u7684\u65b9\u4fbf\uff09\uff0c\u9700\u8981\u505a\u4e00\u4e0b\u8f6c\u5316\u3002<\/p>\n

          \n
            \n
          1. select<\/span> <\/span>year<\/span>,<\/span>month<\/span> GROUP_CONCAT(conv( oct( volumn ) , 8, 10 )) <\/span>from<\/span> magazine <\/span>group<\/span> <\/span>by<\/span> <\/span>year<\/span>,<\/span>month<\/span> <\/span>order<\/span> <\/span>by<\/span> <\/span>year<\/span> <\/span>desc<\/span>, <\/span>month<\/span> <\/span>desc<\/span>  <\/span><\/span><\/li>\n<\/ol>\n<\/div>\n

            <\/font>      \u4e0a\u9762\u7684sql\u5bf9volumn\u505a\u4e86\u4e00\u4e2a\u4ece8\u8fdb\u5236\u523010\u8fdb\u5236\u7684\u8f6c\u6362\uff0c\u8fd9\u6837\u8fd4\u56de\u7684\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e86\u3002<\/p>\n

            mysql\u9ed8\u8ba4\u4f1a\u4ee5‘,’\u6765\u5206\u9694\u591a\u7684\u503c\uff0c\u5982\u679c\u60f3\u7528\u5176\u4ed6\u7684\u5206\u9694\u7b26\u6765\u5206\u9694\u8fd4\u56de\u7ed3\u679c\uff0c\u6bd4\u5982\u671f\u671b\u8fd4\u56de\u503c\u662f\u8fd9\u6837\u7684\uff1a1|2|3|4<\/p>\n

            \u8fd9\u53ef\u4ee5\u7528SEPARATOR\u6765\u641e\u5b9a\u3002<\/font><\/code><\/p>\n

            \n
              \n
            1. select<\/span> <\/span>year<\/span>,<\/span>month<\/span> GROUP_CONCAT(conv( oct( volumn ) , 8, 10 ) SEPARATOR <\/span>‘|’<\/span>) <\/span>from<\/span> magazine <\/span>group<\/span> <\/span>by<\/span> <\/span>year<\/span>,<\/span>month<\/span> <\/span>order<\/span> <\/span>by<\/span> <\/span>year<\/span> <\/span>desc<\/span>, <\/span>month<\/span> <\/span>desc<\/span>   <\/span><\/span><\/li>\n<\/ol>\n<\/div>\n

              \u66f4\u725b\u7684\u662f\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u5bf9\u8fd4\u56de\u7684volumn\u8fdb\u884c\u6392\u5e8f\uff01\uff01<\/p>\n

              \n
                \n
              1. select<\/span> <\/span>year<\/span>,<\/span>month<\/span> GROUP_CONCAT(conv( oct( volumn ) , 8, 10 ) <\/span>order<\/span> <\/span>by<\/span> volumn <\/span>desc<\/span> SEPARATOR <\/span>‘|’<\/span>) <\/span>from<\/span> magazine <\/span>group<\/span> <\/span>by<\/span> <\/span>year<\/span>,<\/span>month<\/span> <\/span>order<\/span> <\/span>by<\/span> <\/span>year<\/span> <\/span>desc<\/span>, <\/span>month<\/span> <\/span>desc<\/span>  <\/span><\/span><\/li>\n<\/ol>\n<\/div>\n

                \u8fd9\u4e2a\u4e0d\u5e38\u7528\u7684\u4e1c\u4e1c\uff0c\u8fd8\u662f\u6bd4\u8f83\u597d\u7528\u7684\u3002<\/p>\n

                \u5f15\u7528\uff1ahttp:\/\/javeye.javaeye.com\/blog\/558093<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

                \u6211\u4eec\u77e5\u9053\uff0cgroup by\u53ef\u4ee5\u5c06sql\u67e5\u8be2\u7ed3\u679c\u6309\u7167group by\u540e\u9762\u5217\u8fdb\u884c\u5206\u7c7b\u663e\u793a\u3002\u6bd4\u5982\uff1a<\/p>\n