在三个表中使用join,但结果显示在很多行中

时间:2013-09-23 07:43:38

标签: asp.net sql sql-server sql-server-2008 ado.net

我有三个表OccupancyType,RoomType和Hotel_MealPlan。数据是: -

OccupancyType数据: -

Hotel_ID    Name
       1    Double
       1    single
     680    Double
     680    single

Hotel_MealPlan

Hotel_ID    Meal_Plan   rate
       1    CP          0
       1    MAP         500
     680    CP          400
     680    EP          400         
       1    EP          200

RoomType

Hotel_ID    Name    Rate    Season    StartSeason   EndSeason
 680    Deluxe/Ac   2300    Diwali   2013-11-01 2013-11-15
 680    Deluxe/Ac   1000    Normal    NULL          NULL
   1    Deluxe/Ac   2700    Diwali    2013-11-01    2013-11-15
   1    Deluxe/Ac   1200    Normal    NULL          NULL
   1    Deluxe/Ac   2500    New Year  2013-12-20    2014-01-10
   1    Deluxe/Ac   3800    31 Dec    2013-12-31    2013-12-31

我希望当我选择hotel_Id'1'时,occupancyType Name ='Double',Meal_Plan ='MAP'和我的roomtype name ='Deluxe / Ac'以及startSeason和EndSeason之间的日期,或者如果开始结束季节为Null则选择null率。我想要的结果如下: - 下面

Name    Name    Rate    startseason endseason   Meal_Plan   rate
Double  Deluxe/Ac   1200    NULL    NULL        MAP         500

我的查询是

select o.Name,r.Name,r.Rate,r.startseason, r.endseason, m.Meal_Plan,m.rate 
from OccupancyType o 
inner join RoomType r on o.Hotel_ID = r.Hotel_ID 
inner join Hotel_MealPlan m on m.Hotel_ID = o.Hotel_ID 
where r.Hotel_ID = '1' 
and r.Name = 'Deluxe/Ac' 
and o.Name = 'Double' 
and m.Meal_Plan = 'MAP' 
and '2013-09-09' between r.startseason and r.endseason 
or r.startseason is null 
and r.endseason is null 

我的结果是: -

Name    Name    Rate    startseason endseason   Meal_Plan   rate
Double  Deluxe/Ac   1200    NULL    NULL    CP          0
Double  Deluxe/Ac   1200    NULL    NULL    MAP         500
Double  Deluxe/Ac   1200    NULL    NULL    EP          200
single  Deluxe/Ac   1200    NULL    NULL    CP          0
single  Deluxe/Ac   1200    NULL    NULL    MAP         500
single  Deluxe/Ac   1200    NULL    NULL    EP          200
Double  Deluxe/Ac   1200    NULL    NULL    CP          0
Double  Deluxe/Ac   1200    NULL    NULL    MAP         600
Double  Deluxe/Ac   1200    NULL    NULL    CP          400
Double  Deluxe/Ac   1200    NULL    NULL    EP          400
Double  Deluxe/Ac   1200    NULL    NULL    MAP         600 ........

我的结果显示48行...我缺少什么?

2 个答案:

答案 0 :(得分:2)

您的联接会显示与之匹配的所有行:

r.Hotel_ID = '1' 
and r.Name = 'Deluxe/Ac' 
and o.Name = 'Double' 
and m.Meal_Plan = 'MAP' 
and '2013-09-09' between r.startseason and r.endseason 

r.startseason is null 
and r.endseason is null 

您可能希望执行以下操作:

WHERE r.Hotel_ID = '1' 
AND r.Name = 'Deluxe/Ac' 
AND o.Name = 'Double' 
AND m.Meal_Plan = 'MAP' 
AND ('2013-09-09' between r.startseason and r.endseason 
    OR (r.startseason is null 
    AND r.endseason is null))

如果有季节比率,这仍然会带来淡季率,但你也可以添加一些顺序,只使用第一行(等等)

答案 1 :(得分:0)

您可以尝试以下内容:

select o.Name,r.Name,r.Rate,r.startseason, r.endseason, m.Meal_Plan,

rate = CASE WHEN.startseason = NULL THEN NULL              当r.endseason = NULL那么NULLL              ELSE m.rate END 来自OccupancyType o 内部联接RoomType r on o.Hotel_ID = r.Hotel_ID和r.Name ='Deluxe / Ac'和o.Name ='Double' 内部加入Hotel_MealPlan m on m.Hotel_ID = o.Hotel_ID and m.Meal_Plan ='MAP' 其中r.Hotel_ID ='1' 和(('2013-09-09'在r.startseason和r.endseason之间)或 或者r.startseason为null 或者r.endseason为null)