MySQL database combines individual fields of multiple records into one record

Source: Internet
Author: User

MySQL database merges a single field of multiple records into one record. This is what we will introduce in this article, next let's take a look at this part. Test TABLE Structure: www.2cto.com create table if not exists 'tet' ('id' int (11) not null, 'name' varchar (255) not null, 'url' varchar (255) not null) ENGINE = InnoDB default charset = utf8; data in the stored table 'tet' insert into 'tet' ('id', 'name ', 'url') VALUES (1, 'Baidu ',' http://www.baidu.com '), (0, 'Google ',' http://www.google.com.hk '), (3, 'phone 400 ',' http://www.my400800.cn '); Method 1: SELECT GROUP_CONCAT (name) name FROM tet WHERE 11 = 1 LIMIT 0, 30 Results: name Baidu, google, 400 phone. GROUP_CONCAT can also use the SEPARATOR keyword to specify the connector. The SQL statement is as follows: SELECT GROUP_CONCAT (url SEPARATOR "@") url FROM tet WHERE 11 = 1 LIMIT 0, 30 results: http://www.baidu.com @ http://www.google.com.hk @ http://www.my400800.cn Method 2: SELECT GROUP_CONCAT (name) name FROM tet WHERE 11 = 1 group by id LIMIT 0, 30 Results: google Baidu www.2cto.com 400 phone 1. test statement: SELECT group_concat (town) FROM 'players' group by town to find out which values are the same in the town. If they are the same, all values are listed, use commas (,) to list data, as follows: group_concat (town) Beijing, Beijing Changsha 2. test: SELECT group_concat (town) FROM players result: group_concat (town) Changsha, Beijing, and Beijing. It can be proved that group_concat can only be used together with group by statements to produce results.

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.