zoukankan      html  css  js  c++  java
  • 行转列group_concat()函数妙用

    测试环境准备:

    mysql> create table test01(name varchar(20),address varchar(20)); 
    Query OK, 0 rows affected (0.28 sec)
    mysql> insert into test01 values('huzj','hunan'); 
    Query OK, 1 row affected (0.06 sec)
    mysql> insert into test01 values('huzj','hubei'); 
    Query OK, 1 row affected (0.00 sec)
    mysql> insert into test01 values('huzj','jianxi'); 
    Query OK, 1 row affected (0.01 sec)
    mysql> insert into test01 values('huzj','sichuan'); 
    Query OK, 1 row affected (0.01 sec)
    mysql> insert into test01 values('huzy','sichuan'); 
    Query OK, 1 row affected (0.00 sec)
    mysql> insert into test01 values('huzy','nibo'); 
    Query OK, 1 row affected (0.01 sec)
    mysql> insert into test01 values('huzy','shanghai'); 
    Query OK, 1 row affected (0.00 sec)
    mysql> insert into test01 values('huzy','beijing'); 
    Query OK, 1 row affected (0.01 sec)
    

     

    测试:

    mysql> select name,group_concat(address) from test01 group by name;
    +------+-------------------------------+
    | name | group_concat(address)         |
    +------+-------------------------------+
    | huzj | hunan,hubei,jianxi,sichuan    |
    | huzy | sichuan,nibo,shanghai,beijing |
    +------+-------------------------------+
    2 rows in set (0.00 sec)
    

    结论:group_concat()函数可以很好的建属于同一分组的多个行转化为一个列

  • 相关阅读:
    游记 Day10
    游记 Day9
    NOIP模拟测试10
    【贪心】P3942 将军令 && P2279 消防局的设立
    在没有上考场之前,菜鸡也有翻盘的机会
    【数据结构】 圆方树&&广义圆方树
    快速幂&&龟速乘&&快速乘
    游记 Day 4
    【容斥】[ZJOI2016] 小星星
    游记 Day3
  • 原文地址:https://www.cnblogs.com/janehoo/p/5583166.html
Copyright © 2011-2022 走看看