导语
这次要实现的是一对多查询,使用 MySQL 的 group_concat 函数实现。
group_concat
简单来说,这个函数的作用就是连接多个字段,函数的具体详解可以看这篇文章。
数据表
首先我们先建立两个表
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'tom');
INSERT INTO `student` VALUES ('2', 'jerry');
CREATE TABLE `course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`s_id` int(11) NOT NULL,
`c_name` char(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6