日期:2014-05-18 浏览次数:20467 次
if object_id('[memberdetails]') is not null drop table [memberdetails] go create table [memberdetails]([memberid] int,[firstname] varchar(2),[lastname] varchar(4)) insert [memberdetails] select 1,'张','效益' union all select 2,'里','说客' union all select 3,'我','我们' union all select 3,'是','速度' if object_id('[category]') is not null drop table [category] go create table [category]([categoryid] int,[category] varchar(5)) insert [category] select 1,'科幻' union all select 2,'动作' union all select 3,'APIAN' if object_id('[favcategory]') is not null drop table [favcategory] go create table [favcategory]([categoryid] int,[memberid] int) insert [favcategory] select 1,1 union all select 1,2 union all select 2,3 union all select 2,4 union all select 3,4 SELECT B.category, A.firstname, A.lastname FROM memberdetails A INNER JOIN favcategory C ON A.memberid = C.memberid INNER JOIN category B ON C.categoryid = B.categoryid WHERE B.category = '动作' DROP TABLE memberdetails DROP TABLE category DROP TABLE favcategory --测试结果: /* category firstname lastname -------- --------- -------- 动作 我 我们 动作 是 速度 (所影响的行数为 2 行) */