读书人

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

发布时间: 2012-03-22 17:43:57 作者: rapoo

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


读书人网 >SQL Server

热点推荐