mysql group_concat method example to write group fields into one row

mysql group_concat method example to write group fields into one row

This article uses an example to describe how to use MySQL group_concat to write grouped fields into one row. Share with you for your reference, the details are as follows:

Function : Connect the values ​​in the same group generated by group by and return a string result.

Function syntax :

group_concat( [DISTINCT] Fields to be connected [Order BY Sorting fields ASC/DESC] [Separator 'Separator'] )

Example

Query the article list and use multiple tags of the same article as one field

Tag table structure

CREATE TABLE `book_tag` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `tag_name` varchar(255) NOT NULL DEFAULT '0' COMMENT 'Tag name',
 `tag_nums` int(10) NOT NULL DEFAULT '0' COMMENT 'Number of references',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Tag article relationship table

CREATE TABLE `book_tag_book` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `book_id` int(10) NOT NULL DEFAULT '0' COMMENT 'Book id',
 `tag_id` int(10) NOT NULL DEFAULT '0' COMMENT 'Tag ID',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query

SELECT
 `b`.`id`,
 `b`.`book_name`,
 `b`.`book_flash`,
 `b`.`introduction`,
 GROUP_CONCAT(a.tag_name)
FROM
 `book_book` `b`
LEFT JOIN `book_tag_book` `t` ON `t`.`book_id` = `b`.`id`
LEFT JOIN `book_tag` `a` ON `a`.`id` = `t`.`tag_id`
GROUP BY b.id

result

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL merges multiple rows of data based on the group_concat() function
  • MySql Group By implements grouping of multiple fields
  • mysql group by grouping multiple fields
  • How to merge and display multiple data of a field after grouping in MySQL

<<:  Implementation of MySQL multi-version concurrency control MVCC

>>:  Centos7 installation of FFmpeg audio/video tool simple document

Recommend

Tutorial on disabling and enabling triggers in MySQL [Recommended]

When using MYSQL, triggers are often used, but so...

How to install vim editor in Linux (Ubuntu 18.04)

You can go to the Ubuntu official website to down...

Analyze the selection problem of storing time and date types in MySQL

In general applications, we use timestamp, dateti...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Best Practices for Developing Amap Applications with Vue

Table of contents Preface Asynchronous loading Pa...

Quickly solve the problem that CentOS cannot access the Internet in VMware

Yesterday I installed CentOS7 under VMware. I wan...

mysql having usage analysis

Usage of having The having clause allows us to fi...

Unzipped version of MYSQL installation and encountered errors and solutions

1 Installation Download the corresponding unzippe...

Detailed explanation of invisible indexes in MySQL 8.0

Word MySQL 8.0 has been released for four years s...

Detailed explanation of jquery tag selector application example

This article example shares the specific code of ...

MySQL cursor functions and usage

Table of contents definition The role of the curs...