日期:2014-05-17  浏览次数:20357 次

一个数据格式问题,在线等,高手来帮帮忙,谢谢了
表1:
  id groupname attribute op value
  1 abc Download := 151-250
  2 abc upload := 50-150
  3 efg Download := 152-250
  4 efg upload := 50-150
怎么将上表中的数据转换成下面的格式
  id groupname Download upload
  1 abc 151-250 50-150
  2 efg 152-250 50-150
SQL:
SELECT a.groupname,a.value AS upload,b.value AS Download FROM radgroupreply a LEFT JOIN radgroupreply b ON a.groupname=b.groupname WHERE a.attribute='upload' AND b.attribute='download' AND a.groupname IN ('abc','efg')
这种方法可以实现,但是感觉效率有点低,有没有更好的办法?
用临时表是不是比上面的方法效率要好点,如果用临时表,怎么将数据插入到临时表中,如果直接create然后执行以上SQL,这样就没意义了,有没有什么好办法进行插入呢?
因为这张表数据比较大,考虑到效率问题,想找到一个更好的解决办法,请各位高手帮帮忙,给点思路,不胜感激!
在线等,急!!!!!

------解决方案--------------------
用存诸过程创建临时表
------解决方案--------------------
你这是求交叉表,有现成的存储过程可供参考。搜索“交叉表”就可找到


------解决方案--------------------
行列转换……
SQL code


set @id="";
select (@id:=@id+1) AS id, groupname,
 group_concat(if(attribute='Download', `value`, '') separator '') AS Download,
 group_concat(if(attribute='upload', `value`, '') separator '') AS upload
 from 表名 group by groupname