首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

哪位大神帮忙看看这种查询该如何实现

2012-08-25 
哪位大神帮忙看看这种查询该怎么实现a表:idname1张三2李四3王五4赵六b表:idclassaId111212323414查询class

哪位大神帮忙看看这种查询该怎么实现
a表:
id name 
1 张三
2 李四
3 王五
4 赵六

b表:
id class aId
1 1 1
2 1 2
3 2 3
4 1 4


查询class=1的所有人的姓名
显示结果为:
class name
1 张三,李四,赵六

[解决办法]
select b.class,group_concat(a.name) from a inner join b
on a.id=b.aid group by b.class
[解决办法]

SQL code
root@localhost : test 04:32:29>select * from a;+------+--------+| id   | name   |+------+--------+|    1 | 张三   ||    2 | 李四   ||    3 | 王五   ||    4 | 赵六   |+------+--------+4 rows in set (0.00 sec)root@localhost : test 04:32:33>select * from b;+------+-------+------+| id   | class | aid  |+------+-------+------+|    1 |     1 |    1 ||    2 |     1 |    2 ||    3 |     2 |    3 ||    4 |     1 |    4 |+------+-------+------+4 rows in set (0.00 sec)root@localhost : test 04:32:34>select class,group_concat(name) as name from a,b where a.id=b.aid and class=1 group by class;+-------+----------------------+| class | name                 |+-------+----------------------+|     1 | 张三,李四,赵六       |+-------+----------------------+1 row in set (0.00 sec)
[解决办法]
GROUP_CONCAT(DISTINCT test)

热点排行