注册 登录
编程论坛 SQL Server论坛

某大公司面试题

qsrock 发布于 2007-08-30 21:58, 982 次点击
一个表的字段有id,name,parent_id(父亲的id),查找做爷爷的人!~
写一个SQL语句.
8 回复
#2
卡卡艾2007-08-31 00:43
我来写一个:
比如要查找ID是001的人的爷爷的信息,如下:
select * from table where id =(select parent_id from table where id = (select parent_id from table where id = '001' ));
#3
卡卡艾2007-08-31 00:45

没错吧.
有标准答案吗?

#4
xiapi2007-08-31 08:50

假设没有父亲信息则parent_id为空

select name from table where id in (select parent_id from table where id in (select parent_id from table where parent_id<>""))

不知道对不对

#5
kaishiuexi2007-09-01 00:05
题目表达不详,有刁难嫌疑.
#6
qy00272007-09-01 10:35
应是这样的码:
select name from table where id in (select parent_id from table where id in (select parent_id from table))
#7
fqbnet20082007-09-03 13:16
楼上正解,我觉得就应该是这样的, 题目也没用什么表达不祥
#8
thllilac2007-09-07 17:24
应是这样的码:
select name from table where id in (select distinct parent_id from table where id in (select distinct parent_id from table))
#9
lthiy2007-09-07 17:57

都是高手
小弟刚学SQL,希望大家多多帮忙,多多指点

1