标签:style records display 连接 closed into one cat ide
group_concat
简单来说,这个函数的作用就是连接多个字段
数据表
首先我们先建立两个表
1 CREATE TABLE `student` ( 2 `id` int(11) NOT NULL AUTO_INCREMENT, 3 `name` char(10) NOT NULL, 4 PRIMARY KEY (`id`) 5 ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 6 7 -- ---------------------------- 8 -- Records of student 9 -- ---------------------------- 10 INSERT INTO `student` VALUES (‘1‘, ‘tom‘); 11 INSERT INTO `student` VALUES (‘2‘, ‘jerry‘); 12 13 CREATE TABLE `course` ( 14 `id` int(11) NOT NULL AUTO_INCREMENT, 15 `s_id` int(11) NOT NULL, 16 `c_name` char(10) NOT NULL, 17 PRIMARY KEY (`id`) 18 ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; 19 20 -- ---------------------------- 21 -- Records of course 22 -- ---------------------------- 23 INSERT INTO `course` VALUES (‘1‘, ‘1‘, ‘语文‘); 24 INSERT INTO `course` VALUES (‘2‘, ‘1‘, ‘数学‘); 25 INSERT INTO `course` VALUES (‘3‘, ‘2‘, ‘英语‘); 26 INSERT INTO `course` VALUES (‘4‘, ‘2‘, ‘体育‘); 27 INSERT INTO `course` VALUES (‘5‘, ‘2‘, ‘美术‘);
下面用 group_concat 函数查询
1 SELECT S.`NAME`,(SELECT GROUP_CONCAT(COURSE.C_NAME) FROM COURSE WHERE COURSE.S_ID = S.ID) FROM STUDENT AS S;
标签:style records display 连接 closed into one cat ide
原文地址:https://www.cnblogs.com/xiaofengfree/p/10205438.html