SQL謎題(樓層謎題)

Multiple Dwellings
Baker, Cooper, Fletcher, Miller and Smith live on different floors of an apartment house that contains only five floors.
Baker does not live on the top floor. Cooper does not live on the bottom floor.
Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper.
Smith does not live on a floor adjacent to Fletcher’s.
Fletcher does not live on a floor adjacent to Cooper’s. Where does everyone live?

--Baker, Cooper, Fletcher, Miller and Smith住在一座房子的不同樓層.

--Baker 不住頂層.

--Cooper不住底層.

--Fletcher 既不住頂層也不住底層.
--Miller住得比Cooper高.
--Smith住的樓層和Fletcher不相鄰.
--Fletcher住的樓層和Cooper不相鄰.

 

 

select*from 
(select (number) Baker from master.dbo.spt_values where type='P' and number between 1 and 5 ) as a,
(select (number) Cooper from master.dbo.spt_values where type='P' and number between 1 and 5 ) as b,
(select (number) Fletcher from master.dbo.spt_values where type='P' and number between 1 and 5 ) as c ,
(select (number) Miller from master.dbo.spt_values where type='P' and number between 1 and 5 ) as d,
(select (number) Smith from master.dbo.spt_values where type='P' and number between 1 and 5 ) as e
where 
Baker<>5 and 
Cooper<>1 and 
Fletcher not in (1,5) and 
Miller>Cooper and 
abs(Smith-Fletcher)<>1 and 
abs(Fletcher-Cooper)<>1
and Baker<>Cooper 
and Baker<>Fletcher
and Baker<>Miller 
and Baker<>Smith
and Cooper<>Fletcher
and Cooper<>Miller
and Cooper<>Smith 
and Fletcher<>Miller
and Fletcher<>Smith
and Smith<>Miller

查詢結果

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章