Use of the MYSQL function group_concat

Source: Internet
Author: User


The MYSQL function group_concat is used to migrate and convert a batch of data today. After checking the function, group_concat is used to easily implement column-to-row setting of fields. The procedure is recorded as follows. test data preparation mysql> use test; Database changedmysql> select * from t_kenyon; + ------ + | id | + ------ + | 1 | 123 | 789 | 345 | 78 | + ------ + 5 rows in set (0.00 sec) II. use through www.2cto.com 1. use the default comma as the separator mysql> select group_concat (id) from t_kenyon; + ---------------- + | group_concat (id) | + ------------------ + | 1,123,789,345, 78 | + ------------------ + 1 row in set (0.00 sec) 2. sort the ID values and convert them to mysql> select group_concat (id order by id) from t_kenyon; + consumer + | group_concat (id order by id) | + ---------------------------- + |, 123,345,789 | + ------------------------------ + 1 row in set (0.00 sec) 3. use other delimiters, such as * And;. mysql> select group_concat (id separator '*') from t_kenyon; + ---------------------------- + | group_concat (id separator '*') | + ------------------------------ + | 1*123*789*345*78 | + ---------------------------------- + 1 row in set (0.00 sec) www.2cto.com
4. use mysql> select group_concat (id order by id separator '_') from t_kenyon; + ------------------------------------------------ + | group_concat (id order by id separator '_') | + ---------------------------------------- + | 201778_123_345_789 | + -------------------------------------------- + 1 row in set (0.00 sec) 5. mysql> insert into t_kenyon values (78); Query OK, 1 row affected (0.00 sec) mysql> select group_concat (id) from t_kenyon group by id; + ------------------ + | group_concat (id) | + ------------------ + | 1 | 78,78 | 123 | 345 | 789 | + ---------------- + 5 rows in set (0.00 sec)
III. parameter settings and restrictions 1. check that www.2cto.com mysql> show variables like '% group_concat %' is set on the server '; + ---------------------- + ------- + | Variable_name | Value | + ---------------------- + ------- + | group_concat_max_len | 1024 | + ---------------------- + ------- + 1 row in set (0.00 sec) the above value indicates that the current default length is 1 KB 2. method 1: Modify parameters in the configuration file and add group_concat_max_len = 10240. Method 2: set global group_concat_max_len = 10240 globally or in the current session; set session group_concat_max_len = 10240; prepared by kenyon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.