各位..先谢谢你们啦.我没什么分.
有 1表 .
表是 'book'
字段为 id,tid,optionid,value有下列数据id tid optionid value
1 1 1 a
2 1 2 b
3 1 3 c
4 1 4 3 <---- optionid 无 55 2 2 c <---- optionid 无 1
6 2 3 d
7 2 4 ce
8 2 5 3将 tid 组合. 按 optionid 的数字 为 字段并加入字段名 .
tid 标题(1) 内容(2) 作者(3) 租价(4) 售价(5)
1 a b c 3
2 c d ce 3
有 1表 .
表是 'book'
字段为 id,tid,optionid,value有下列数据id tid optionid value
1 1 1 a
2 1 2 b
3 1 3 c
4 1 4 3 <---- optionid 无 55 2 2 c <---- optionid 无 1
6 2 3 d
7 2 4 ce
8 2 5 3将 tid 组合. 按 optionid 的数字 为 字段并加入字段名 .
tid 标题(1) 内容(2) 作者(3) 租价(4) 售价(5)
1 a b c 3
2 c d ce 3
GROUP_CONCAT(optionid SEPARATOR ' ')
FROM tb1 GROUP BY tid ;
GROUP_CONCAT(optionid SEPARATOR ' ')
FROM tb1 GROUP BY tid ;1楼有问题。
max(if(optionid=1,value,null)) as `标题`,
max(if(optionid=2,value,null)) as `内容`,
max(if(optionid=3,value,null)) as `作者`,
max(if(optionid=4,value,null)) as `租价`,
max(if(optionid=5,value,null)) as `售价`
from book
group by tid
GROUP_CONCAT(value SEPARATOR ' ')
FROM tb1 GROUP BY tid ;2楼写错字段了。
狼头哥的答案很可能就是楼主要的,行列转换的。
max(case when optionid=2 then value else '' end) as `内容`,
max(case when optionid=3 then value else '' end) as `作者`,
max(case when optionid=4 then value else '' end) as `租价`,
max(case when optionid=5 then value else '' end) as `售价`
from book
group by tid