日期:2014-05-17 浏览次数:20450 次
create table user_table(id int,[user_name] nvarchar(10))
insert into user_table values (1,'爷爷')
insert into user_table values (2,'爸爸')
insert into user_table values (3,'儿子')
create table user_relation_table(id int, user1_id int,user2_id int,[state] nvarchar(10))
insert into user_relation_table values(1,1,2,'父-子')
insert into user_relation_table values(2,1,3,'爷-孙')
insert into user_relation_table values(3,2,3,'父-子')
select urt.state
from user_relation_table urt
LEFT JOIN user_table ut1
on urt.user1_id=ut1.id
INNER JOIN user_table ut2
on urt.user2_id=ut2.id
where ut1.[user_name]='爷爷' and ut2.[user_name]='儿子'
/*
state
爷-孙
*/