2013-03-08 57 views
1

我有4个表:连接多个表中的值到一个表,以看得清

订单:

OrderID Detail Discount 
------------------------ 
1001 xxx  True 
1002 xxx  True 
1003 xxx  True 
1004 xxx  False 
1005 xxx  True 
1006 xxx  True 

OrderDiscounts:

OrderID DiscountTypeID DiscountID 
---------------------------------- 
1001  1    8 
1002  2    12 
1003  1    9 
1005  2    13 
1006  2    9 

DiscountTypeID = 1):

CouponID Title 
------------------------ 
8   CouponTitle8 
9   CouponTitle9 

广告活动DiscountTypeID = 2):

CampaignID Title 
-------------------------- 
9   CampaignTitle9 
12   CampaignTitle12 
13   CampaignTitle13 

我需要一个查询,将所有4个表合并为1台,这将给像一些结果:

结果:

OrderID Discount DiscountType DiscountTitle 
----------------------------------------------------- 
1001 True  Coupon   CouponTitle8 
1002 True  Campaign  CampaignTitle12 
1003 True  Coupon   CouponTitle9 
1004 False    
1005 True  Campaign  CampaignTitle13 
1006 True  Campaign  CampaignTitle9 

请注意,某些优惠券ID可能作为广告系列ID存在。在这种情况下,就像CouponID和CampaignID一样,存在'9'。

除了需要查询之外,关于如何/为什么在构建查询时使用这些命令的适当解释将非常棒,因为我不只是寻找答案,但我想自己也处理类似的情况。谢谢!

+0

DiscountType从哪里来? – 2013-03-08 20:31:11

+0

嘲笑它。尝试获得至少一个部分解决方案。告诉我们你已经知道了什么。 – 2013-03-08 20:34:42

+0

@Pieter Geerkens来自OP的帖子:**优惠券**:('DiscountTypeID' = 1)和**广告系列**:('DiscountTypeID' = 2) – 2013-03-08 20:37:58

回答

1

应该类似于下:

SELECT Orders.OrderID, 
CASE when OrderDiscounts.OrderID is NULL THEN False ELSE True end, 
CASE when OrderDiscounts.DiscountTypeID = 1 THEN 'Coupon' 
    when OrderDiscounts.DiscountTypeID = 2 THEN 'Campaign' 
    else '' end, 
CASE WHEN OrderDiscounts.DiscountTypeID = 1 THEN Coupons.Title 
    WHEN OrderDiscounts.DiscountTypeID = 2 THEN Campaigns.Title 
    ELSE '' end 
FROM Orders 
LEFT JOIN OrderDiscounts on Orders.OrderID = OrderDiscounts.OrderID 
LEFT JOIN Coupons on OrderDiscounts.DiscountID = Coupons.CouponID 
LEFT JOIN Campaigns on OrderDiscounts.DiscountID = Campaigns.CampaignID 
+0

我删除了'CONVERT'语句,并能够很好地运行该示例。在没有转换的情况下运行它会导致什么错误? – brazilianldsjaguar 2013-03-08 22:55:06

+0

Grr。我也刚刚做到了。一切都很好。傻我是:/你的图式+1。 – www 2013-03-08 22:57:08

2

下面的代码应该工作 - 我与稍加修改做到了:我添加了一个“DiscountType”表。

我明白,也许你正在寻找的只是一个快速和简单的答案,但我认为数据模式可以设置得更好。对于每种折扣类型,整个表格都不具有伸缩性和灵活性,并且随着时间的推移,像所建议的那样的查询将只会变得更加毛茸茸,更加晦涩,更加单调。当然,这一切都取决于你的要求。

SELECT 
    OrderId = o.OrderId, 
    Discount = CASE WHEN o.Discount = 1 THEN 'True' ELSE 'False' END, 
    DiscountType = COALESCE(dt1.DiscountType, dt2.DiscountType, ''), 
    DiscountTitle = COALESCE(coup.Title, camp.Title, '') 
FROM 
    Orders o 
    LEFT JOIN 
    (OrderDiscounts od1  
    JOIN Coupons coup ON coup.CouponID = od1.DiscountID 
    LEFT JOIN DiscountType dt1 ON dt1.DiscountTypeId = od1.DiscountTypeId 
    ) ON o.OrderId = od1.OrderId 
     AND o.Discount = 1 
     AND od1.DiscountTypeID = 1 
    LEFT JOIN 
    (OrderDiscounts od2 
    JOIN Campaigns camp ON camp.CampaignID = od2.DiscountID 
    LEFT JOIN DiscountType dt2 ON dt2.DiscountTypeId = od2.DiscountTypeId 
    ) ON 
     od2.OrderID = o.OrderID 
     AND o.Discount = 1 
     AND od2.DiscountTypeID = 2 

在SqlFiddle看你自己here

相关问题