Home >Database >Mysql Tutorial >mysql implements the method of merging multiple pieces of data corresponding to the same ID_MySQL
The example in this article describes the method of mysql to merge multiple pieces of data corresponding to the same ID. Share it with everyone for your reference, the details are as follows:
such as:
CREATE TABLE `c_classuser_tab` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Classid` int(11) DEFAULT NULL, `Username` varchar(100) DEFAULT NULL, `studentid` varchar(100) DEFAULT NULL, `College` varchar(100) DEFAULT NULL, `Department` varchar(100) DEFAULT NULL, `Email` varchar(100) DEFAULT NULL, `Phone` varchar(100) DEFAULT NULL, `Status` int(11) DEFAULT NULL, `Remark1` varchar(100) DEFAULT NULL, `Remark2` varchar(100) DEFAULT NULL, `Remark3` varchar(100) DEFAULT NULL, `activateTime` datetime DEFAULT NULL, `joinTime` datetime DEFAULT NULL, `userID` int(11) DEFAULT NULL, `userType` int(11) DEFAULT NULL, PRIMARY KEY (`Id`), KEY `C_classuser_tab_index` (`studentid`), KEY `idx_classuser_complex` (`Classid`,`userID`) ) ENGINE=MyISAM AUTO_INCREMENT=31091 DEFAULT CHARSET=utf8;
Teachers with the same ID have multiple substitute classes, merge them,
Teacher Substitute Class
12 Class 1, Class 2
The code is as follows:
select Username , GROUP_CONCAT(Remark1 ,'') as classes from c_classuser_tab where userID= * and userType=*
Readers who are interested in more MySQL-related content can check out the special topics on this site: "A Complete Collection of MySQL Log Operation Skills", "A Summary of MySQL Transaction Operation Skills", "A Complete Collection of MySQL Stored Procedure Skills", and "A Summary of MySQL Database Lock Related Skills" 》and《Summary of commonly used functions in MySQL》
I hope this article will be helpful to everyone in MySQL database planning.