Chinaunix首页 | 论坛 | 博客
  • 博客访问: 560341
  • 博文数量: 86
  • 博客积分: 2581
  • 博客等级: 少校
  • 技术积分: 793
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-05 20:09
文章分类

全部博文(86)

文章存档

2009年(86)

我的朋友

分类: Mysql/postgreSQL

2009-04-06 11:23:30

id, student, course, mark
1,  张三,    语文,  90
2,  李四,    语文,  80
3,  张三,    数学,  70
4,  李四,    数学,  80

现在想用转置表的方法转成二维表输出,要求格式是:

student, 语文, 数学
张三,    90,  70
李四,    80,  80



select student,
    sum(if(course='语文',mark,0)) as 语文,
    sum(if(course='数学',mark,0)) as 数学
from table1
group by student

1   a
2   b
1   c
2   d
合并为
1   a,c
2   b,d


select group_concat(column_b,separator ',') from table_name group by column_a;

阅读(6840) | 评论(0) | 转发(0) |
0

上一篇:索引设计原则

下一篇:mysql复制表

给主人留下些什么吧!~~