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

请问sql的一个查询语句

2012-02-21 
请教sql的一个查询语句我有一张表tbid tnametcontentbz1aa这是内容null2bb2的13cc3的1我想通过一条sql语句

请教sql的一个查询语句
我有一张表tb
id tname tcontent bz
1 aa 这是内容 null
2 bb 2的 1
3 cc 3的 1


我想通过一条sql语句 实现查询出来的效果为
id tname tcontent bz tcontent2
1 aa 这是内容 null null
2 bb 2的 1 这是内容 
3 cc 3的 1 这是内容 

也就是当bz与ID的值相等时,把ID对应的tcontent内容复制一列出来
怎么实现呢?谢谢高人指点

[解决办法]

SQL code
select a.*,b.tcontent as tcontent2from tb a left join tb b on a.bz=b.id
[解决办法]
SQL code
select  a.*,b.tcontent as tcontent2from  tb a left join tb bon  a.bz=b.id
[解决办法]
select t.* , (select tcontent from tb where bz = t.id) tcontent2 from tb t
[解决办法]
SQL code
create table tb(id int,tname varchar(20) , tcontent varchar(20) , bz int)insert into tb values(1 ,'aa', '这是内容' ,null)insert into tb values(2 ,'bb', '2的'     ,1)insert into tb values(3 ,'cc', '3的'     ,1)goselect t.* , (select tcontent from tb where id = t.bz) tcontent2 from tb tdrop table tb/*id          tname                tcontent             bz          tcontent2            ----------- -------------------- -------------------- ----------- -------------------- 1           aa                   这是内容                 NULL        NULL2           bb                   2的                   1           这是内容3           cc                   3的                   1           这是内容(所影响的行数为 3 行)*/ 

热点排行