请问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内容复制一列出来
怎么实现呢?谢谢高人指点
------解决方案--------------------
我有一张表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 tcontent2 from tb a left join tb b on a.bz=b.id
------解决方案--------------------
- SQL code
select a.*,b.tcontent as tcontent2 from tb a left join tb b on 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) go select t.* , (select tcontent from tb where id = t.bz) tcontent2 from tb t drop table tb /* id tname tcontent bz tcontent2 ----------- -------------------- -------------------- ----------- -------------------- 1 aa 这是内容 NULL NULL 2 bb 2的 1 这是内容 3 cc 3的 1 这是内容 (所影响的行数为 3 行) */