------解决方案-------------------- http://blog.csdn.net/acmain_chm/article/details/4283943 MySQL交叉表 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义。http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4cf5-bc1a-e8914aaee5ab.html?96198现整理解法如下:数据样本: create table tx( id int primary key, c1 c...
------解决方案--------------------
SQL code
SET @oid=0;
SET @oflag_a='';
SET @oflag_b='';
CREATE TEMPORARY TABLE temp_1 AS
SELECT @oid:=IF(@oflag_a<>a OR @flag_b<>b,1,@oid+1) AS oid
-- ,@oid:=@oid+1 AS oid
,@oflag_a:=a,@oflag_b:=b
,id,a,b,c,d,f
FROM(
SELECT id,a,b,c,d,f
FROM testd
ORDER BY a,b
) AS outtb
SELECT a,MAX(CASE WHEN oid=1 THEN CONCAT(b,' ',d,' ',f) ELSE '' END ) AS col1
,MAX(CASE WHEN oid=2 THEN CONCAT(b,' ',d,' ',f) ELSE '' END ) AS col2
,MAX(CASE WHEN oid=3 THEN CONCAT(b,' ',d,' ',f) ELSE '' END ) AS col3
FROM temp_1
WHERE oid<=3
GROUP BY a