首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请问一个联表查询SQL的写法

2011-12-10 
请教一个联表查询SQL的写法比如有2个表:tableAA_idA_textB_id1a112a2null3a3null4a42tableBB_idB_text1 b1

请教一个联表查询SQL的写法
比如有2个表:
table   A
A_id   A_text             B_id  
1a11
2a2null
3a3null
4a42

table   B
B_id         B_text
1 'b1 '
2 'b2 '

A的B_id字段是对应B表的B_id的,但是可能为空,需要得到的结果是将A中每行对应的B_text查询出来,有就是B_text的实际值,如果为空就写 '无 '或者其他之类的.也就是下面的结果
A_id       A_text           B_id       b_text
1a11 'b1 '
2a2null '无 '
3a3null '无 '
4a42 'b2 '

Sql   Server的数据库

[解决办法]
select A.*,B_id,isnull(B_text, '无 ') as B_text from A left join B on A_id=B_id
[解决办法]
楼上的方法最简

select A.A_id,A.A_text,A.B_id,isnull(B.b_text, '无 ') from A left Join B on A.B_id=B.B_id

热点排行