首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

在线急等 两个表之间怎么交互的有关问题

2012-03-22 
在线急等两个表之间如何交互的问题tab_newsidtitlecontenttab_bigclass_id1股票上扬...12国家主席...23美

在线急等 两个表之间如何交互的问题
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


热点排行