Asp.net 在三个表中使用join,但结果显示在许多行中
我有三种职业:桌上型、房间型和旅馆型。数据如下:- 职业类型数据:-Asp.net 在三个表中使用join,但结果显示在许多行中,asp.net,sql,sql-server,sql-server-2008,ado.net,Asp.net,Sql,Sql Server,Sql Server 2008,Ado.net,我有三种职业:桌上型、房间型和旅馆型。数据如下:- 职业类型数据:- Hotel_ID Name 1 Double 1 single 680 Double 680 single 米尔普兰酒店 Hotel_ID Meal_Plan rate 1 CP 0 1 MAP 500 680 CP 400
Hotel_ID Name
1 Double
1 single
680 Double
680 single
米尔普兰酒店
Hotel_ID Meal_Plan rate
1 CP 0
1 MAP 500
680 CP 400
680 EP 400
1 EP 200
房间类型
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
当我选择酒店Id“1”、职业类型名称='Double”、用餐计划='MAP'和我的房间类型名称='Deluxe/Ac'以及开始季节和结束季节之间的日期,或者如果开始季节和结束季节为空,则选择空费率。我希望结果如下:-所示
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行显示在我的结果上。。。我缺少什么?您的联接将显示所有匹配的行:
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
或
您可能希望执行以下操作:
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))
如果有季节费率,这仍然会带来淡季费率,但您也可以添加一些订单,只使用第一行(等)。您可以尝试以下方式:
select o.Name,r.Name,r.Rate,r.startseason, r.endseason, m.Meal_Plan,
速率=r.startseason=NULL时的情况,然后为NULL
当r.endseasure=NULL时,则为NULL
否则m.rate结束
来自职业类型o
o.Hotel_ID=r.Hotel_ID,r.Name='Deluxe/Ac'和o.Name='Double'上的内部连接RoomType r
内部连接Hotel\u MealPlan m on m.Hotel\u ID=o.Hotel\u ID和m.Dine\u Plan='MAP'
其中r.Hotel_ID='1'
(r.startseason和r.endseason之间的“2013-09-09”)或
或r.startseason为空
或者r.endseason为空)它正在工作,但如果我选择任何日期,如“2013-11-11”或“2013-12-31”。只有在赛季开始和赛季结束的时候才有选择率null@Shiv:更新了答案。也许这会对你有所帮助,但如果我选择“2013-11-11”这样的日期。然后显示条件空值和日期,然后显示两行。如果选择“2013-12-31”,则显示三行空值,日期介于2013-12-20至2014-01-10和2013-12-31至2013-12-31之间。如果我选择2013-12-31,那么我想要mealplan 3800 show的费率,请帮助我。因此,我提到您应该应用适用性订单,并且只使用最适用的值。更好的做法是总是定义开始/结束日期(即使在未来很长的一段时间内),我也会尝试:-选择前1。。。。。。。。和('2013-09-09'在r.startseason和r.endseason之间,或(r.startseason为空,r.endseason为空))在r.startseason为空,r.endseason为空时,按情况下订单,然后2个其他1个结束。但在这个查询中,选择2500
select o.Name,r.Name,r.Rate,r.startseason, r.endseason, m.Meal_Plan,