日期:2014-05-18  浏览次数:20419 次

在线急等 两个表之间如何交互的问题
tab_news       id       title         content     tab_bigclass_id
                 
                      1         股票上扬     ...             1
                      2         国家主席     ...             2
                      3         美国总统     ...             3
 
 
tab_bigclass       id     class
                       
                              1       新闻中心
                              2       国内新闻
                              3       国际新闻
                              4       最新动态
                             
需要显示的格式是这样的:
                             
                              [新闻中心]   股票上扬
                              [国内新闻]   国家主席                              
                              [国际新闻]   美国总统

------解决方案--------------------
declare @tab_news table(id int, title varchar(100),tab_bigclass_id int)
declare @tab_bigclass table(id int, class varchar(100))

insert into @tab_news
select 1, '股票上扬 ' , 1 union all
select 2, '国家主席 ' , 2 union all
select 3, '美国总统 ' , 3

insert into @tab_bigclass
select 1, '新闻中心 ' union all
select 2, '国内新闻 ' union all
select 3, '国际新闻 ' union all
select 4, '最新动态 '


select '[ '+b.class+ '] '+a.title
from @tab_news a,@tab_bigclass b
where a.tab_bigclass_id=b.id
------解决方案--------------------
select '[ '+rtrim(title)+ '] '+rtrim(class) from tab_news,tab_bigclass where tab_news.tab_bigclass_id=tab_bigclass.id order by tab_news.id
------解决方案--------------------
select '[ '+clase+ '] ',title from tab_bigclass a left join tab_news b on a.id=b.tab_bigclass_id
order by a.id

这样应该可以吧。
------解决方案--------------------
select '[ '+ltrim(rtrim(b.class))+ '] '+a.title
from @tab_news a,@tab_bigclass b
where a.tab_bigclass_id=*b.id