2013-05-28 47 views
0

添加到SQL结果比如我有一个表:如何一列从其他行中同一个表

num | value | info 
1 | a  | test2 
2 | a  | test1 
1 | b  | specialinfo 
3 | a  | test3 

而且我有一个查询

select * 
from t 
where value='a' 

这将导致

1 a test2 
2 a test1 
3 a test3 

但我想为b添加info作为不是新行,而是作为附加列,所以它会如:

1 a test2 specialinfo 
2 a test1 null 
3 a test3 null 

因此值specialinfo作为附加列而不是附加行。

对于这种情况select * from t where value IN ('a','b')将无法​​正常工作

是有可能做到这一点?

回答

1

为了了解详细内容在一个单独列做这将是多次参加在桌子上得到的结果的一种方法:

select t1.num, t1.value, t1.info, t2.info as t2_info 
from yt t1 
left join yt t2 
    on t1.num = t2.num 
    and t1.value <> t2.value 
where t1.value = 'a'; 

SQL Fiddle with Demo

相关问题