sql查询将用户数据放在一行中

时间:2016-09-06 09:48:23

标签: sql codeigniter

username  skill      mark

x          writing   10
x          reading   11

a          writing   15
a         reading    14

我希望得到这样的o / p:

x       10   11
y       15  14   

2 个答案:

答案 0 :(得分:1)

条件聚合是要走的路:

SELECT t.username,
       MAX(CASE WHEN t.skill = 'writing' THEN t.mark END) as writing_mark,
       MAX(CASE WHEN t.skill = 'reading' THEN t.mark END) as reading_mark
FROM YourTable t
GROUP BY t.username

答案 1 :(得分:0)

select username,skill,mark from Your_table;#this fetches only 3

Or 

select * from Your_table; #this will fetch all fields of your table