本人请人做了一个购物网站,由于商品数量太多,逐一设置太麻烦,现求批量处理的语句,先谢过。
表Products中有ProductID、Saleprice、ProductName等列,表ProductTempletPrices中有TempletID、ProductID、SalePrice这三列,TempletID是价格模板的代号,分别有2、3、4、5、6 ,这分别代表不同级别的价格。我的问题是:如何把表Products中的Saleprice添加到表ProductTempletPrices中对应的Saleprice,两个表ProductID是相互对应的,ProductID在TempletID为2的时候价格即为Products中的Saleprice的价格,TempletID为3的时候价格即为Products中的Saleprice的价格减去10,在TempletID为4的时候价格即为Products中的Saleprice的价格减去20,在TempletID为5的时候价格即为Products中的Saleprice的价格减去30,在TempletID为6的时候价格即为Products中的Saleprice的价格减去40,ProductName中有“特价”字样的维持表Products中的原价。
表Products中有ProductID、Saleprice、ProductName等列,表ProductTempletPrices中有TempletID、ProductID、SalePrice这三列,TempletID是价格模板的代号,分别有2、3、4、5、6 ,这分别代表不同级别的价格。我的问题是:如何把表Products中的Saleprice添加到表ProductTempletPrices中对应的Saleprice,两个表ProductID是相互对应的,ProductID在TempletID为2的时候价格即为Products中的Saleprice的价格,TempletID为3的时候价格即为Products中的Saleprice的价格减去10,在TempletID为4的时候价格即为Products中的Saleprice的价格减去20,在TempletID为5的时候价格即为Products中的Saleprice的价格减去30,在TempletID为6的时候价格即为Products中的Saleprice的价格减去40,ProductName中有“特价”字样的维持表Products中的原价。
主要是想看看你的目的表的PRODUCTID和TEMPLETID的设计是怎么体现的。
怎么体现在目的表的
原表是HIshop_Products,也就是目的表是ProductTempletPrices
为什么目的表,却只有1个,那我到底以哪个TEMPID为准,不知道你明白我意思没有。
但是你要求在目的表中,分别对应2,3,4,5,6这5个级别,根据你提供的运算需求,插入5条记录是这个意思?
这就是问题的难点啊,TempletID为2插入一组数据,TempletID为3时一组数据,为4、5、6时都是一组数据,这所有的数据都保存到 ProductTempletPrices这个表中,这上面的数据是我在网站后台添加的两个商品的不同级别的价格,举例子说吧,2代表普通会员,3代表四级会员,4代表三级会员…………
就是这个意思,每个PRODUCTID对应五条记录,TempletID分别是2、3、4、5、6的时候
declare @Products table
(
ProductID int,
Saleprice decimal(10,2),
ProductName varchar(100)
)declare @ProductTempletPrices table
(
TempletID int,
ProductID int,
SalePrice decimal(10,2)
)insert @Products select 48,260.00,'佐丹奴男装1'
union all select 49,212.00,'佐丹奴男装2'
union all select 50,31.9,'佐丹奴男装3特价'
insert @ProductTempletPrices
select
b.TempletId,
a.ProductID,
SalePrice = case when a.ProductName like '%特价%' then a.SalePrice
else
case
when b.TempletId = 2 then a.SalePrice
when b.TempletId = 3 then a.SalePrice - 10
when b.TempletId = 4 then a.SalePrice - 20
when b.TempletId = 5 then a.SalePrice - 30
when b.TempletId = 6 then a.SalePrice - 40
end
end
from @Products a
cross join(
select 2 as TempletId
union all
select 3 as TempletId
union all
select 4 as TempletId
union all
select 5 as TempletId
union all
select 6 as TempletId
)b
order by b.TempletId,a.ProductID
select * from @ProductTempletPrices/**
2 48 260.00
2 49 212.00
2 50 31.90
3 48 250.00
3 49 202.00
3 50 31.90
4 48 240.00
4 49 192.00
4 50 31.90
5 48 230.00
5 49 182.00
5 50 31.90
6 48 220.00
6 49 172.00
6 50 31.90
**/
是不是你要的效果。