求mysql语句

一个表中有若干数据:
create table test(a int, b int, c int);
a值与b值形成类似迪卡尔积的形式:

a0 b0 c00 
a0 b1 c01 
...
a0 bj c0j 
a1 b0 c10 
a1b1 c11 
...
a1 bj c1j 
...
ai b0 ci0 
ai b1 ci1 
...
ai bj cij 

i,j的范围可能会变动(但一般小于10).如何通过sql语句来动态生成一个新表
  b0 b1 b2 ... bj
----------------------------------------------------------
a0 | c00 c01 c02 ... c0j
a1 | c10 c11 c12 ... c1j
... | ...
ai | ci0 ci1 ci2 ... cij

简单的以i=2, j=3为例:
a0 b0 c00
a0 b1 c01
a0 b2 c02
a1 b0 c10
a1 b1 c11
a1 b2 c12

期望sql语句可以自动生成新表:
  b0 b1 b2 
----------------------------------------------------------
a0 | c00 c01 c02
a1 | c10 c11 c12

SQL code

create table test ( a int, b int, c int);
insert into test values (0, 0, 1);
insert into test values (0, 1, 2);
insert into test values (0, 2, 3);
insert into test values (1, 0, 10);
insert into test values (1, 1, 20);
insert into test values (1, 2, 30);



搞半天,实在没有什么好办法,希望各位达人可以有非常漂亮的解决办法

(我是在PHP代码里面, 先查询 a, 再生用php组织sql语句来完成最终查询,
PHP code

select distinct a from test;  // 得到全部a的值
select distinct b from test;

foreach($a)
select * from (
  (select t1.a from test t1 where a=$a and b=$b0) as b1, 
  (select t2.a from test t2 where a=$a and b=$b1) as b2,
  (select t3.a from test t3 where a=$a and b=$b3) as b2,
...
);


效果非常差,几乎无法使用.)

作者: lisunlin0   发布时间: 2011-06-11

-- 请参考下面链接的 第4小段:

http://blogold.chinaunix.net/u3/90603/showart_2017912.html

作者: luoyoumou   发布时间: 2011-06-11

参考下贴中的各种方法。

MySQL交叉表
http://blog.csdn.net/ACMAIN_CHM/archive/2009/06/19/4283943.aspx

作者: ACMAIN_CHM   发布时间: 2011-06-11