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

select 嵌套的有关问题

2012-01-19 
select 嵌套的问题table1 三个字段idnametoid1你好22中国1select id, (select name from table1 where id

select 嵌套的问题
table1 三个字段
id name toid
1 你好 2
2 中国 1

select id, (select name from table1 where id = toid ) as name from table1
如果这样写,得到的结果只有
1
2
因为name字段永远是空值。

我的问题是,如何让 select 从句中的id 指向 父句中的id,得到的结果应是:
1 中国
2 你好

表达可能不太清楚,见谅。


[解决办法]

SQL code
select id, (select name from table1 where id = t.toid ) as name from table1 t
[解决办法]
SQL code
select a.id,b.name from tb a join tb b on a.id=b.toid 

热点排行