日期:2014-05-16  浏览次数:20431 次

【求助】求多表联合查询SQL
如下图所示:由图1和图2中的两个表联合查询,输出图3所示的结果

图1

图2

图3
------解决方案--------------------
select 
       SUBSTRING(t.author, number ,CHARINDEX(',',t.author+',',number)-number) author,
       copyfrom,
       title,
       updatetime
from 
(
select t1.id,replace(t1.author,' ',',') author,t1.copyfrom,
       t2.title,t2.updatetime
from t1
inner join t2
        On t1.id= t2.generalid 
)t,master..spt_values s
where s.number >=1
and s.type = 'P'
and SUBSTRING(','+t.author,s.number,1) = ','
order by author desc
/*
author copyfrom title updatetime
张三 A公司 aaaaaa 2014-03-01 00:00:00.000
张三 A公司 bbbbbb 2014-03-05 00:00:00.000
张三 A公司 cccccc 2014-03-15 00:00:00.000
王五 A公司 cccccc 2014-03-15 00:00:00.000
王五 A公司 bbbbbb 2014-03-05 00:00:00.000
李四 A公司 bbbbbb 2014-03-05 00:00:00.000
李四 A公司 aaaaaa 2014-03-01 00:00:00.000
*/