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:
|
<<: Implementation of MySQL multi-version concurrency control MVCC
>>: Centos7 installation of FFmpeg audio/video tool simple document
1. One-click installation of Mysql script [root@u...
Problem Description In the recent background serv...
Hardware View Commands system # uname -a # View k...
The previous article was a simple review of the B...
The office needs Ubuntu system as the Linux devel...
When MySQL queries tens of millions of data, most...
Add inline styles to the required links: Copy code...
The traditional method is to write a square in a ...
As we all know, the CSS position absolute is set ...
CentOS official website address https://www.cento...
I saw that Taobao’s webpage uses import, while man...
Table of contents 1. Overview 2. Digital Enumerat...
1. Download the mysql-5.7.17-winx64.zip installat...
Table of contents 1. Basic SELECT statement 1. Qu...
Table of contents What is MVCC Mysql lock and tra...