2012-10-22 44 views
0

我有10000行更新目录值为来自同一个表

的product_id一个简单的表|价格| price_inc_vat | VAT_RATE

我希望能够更新price_inc_vat的基于价格的价值值

我已经试过这

UPDATE 1_products AS s3, 
     1_products AS t3 

SET t3.price_inc = ROUND(s3.price*1.3,2) 

WHERE s3.vat_rate = 20 AND t3.vat_rate=20; 

它没有错误,但似乎花了很长时间因为我必须在1/2小时后杀死查询。

+0

爱contentents –

回答

1

让你有price和现场另一为price_inc_vat,但另一个为vat_rate?为什么不直接存储pricevat_rate并在需要时计算price_inc_vat

此外,为什么当您定位的增值税为20%时,您乘以1.3?

抛开所有这些逻辑不一致,难道你只是做下面的事情吗?

UPDATE products 
SET price_inc_vat = ROUND(price * 1.3, 2) 
WHERE vat_rate = 20 

,或者更新增值税变更后的一切,不顾10%的标记:

UPDATE products 
SET price_inc_vat = ROUND(price * (1+vat_rate/100), 2) 
2

为什么加入?

UPDATE products SET price_inc_vat = ROUND(price*1.3,2) WHERE vat_rate=20; 

修订 我不太清楚你想什么通过更新产品的“公司价格”与其他产品的价格来实现的,但是,你的查询花费这么长时间的原因是因为你加入的方式。

mysql> create table t1 (product_id integer unsigned primary key, price decimal(10,2), price_inc_vat decimal(10,2), vat_rate integer unsigned); 

mysql> insert into t1 values (1, 1.00, 1.20, 20), (2, 2.00, 2.40, 20), (3, 1.00, 1.30, 30); 

mysql> select * from (t1 as s3, t1 as t3) where s3.vat_rate=20 and t3.vat_rate=20; 
1 1.00 1.20 20 1 1.00 1.20 20 
2 2.00 2.40 20 1 1.00 1.20 20 
1 1.00 1.20 20 2 2.00 2.40 20 
2 2.00 2.40 20 2 2.00 2.40 20 

mysql> select s3.*, t3.*, round(s3.price * 1.3, 2) from (t1 as s3, t1 as t3) where s3.vat_rate=20 and t3.vat_rate=20; 
1 1.00 1.20 20 1 1.00 1.20 20 1.30 
2 2.00 2.40 20 1 1.00 1.20 20 2.60 
1 1.00 1.20 20 2 2.00 2.40 20 1.30 
2 2.00 2.40 20 2 2.00 2.40 20 2.60 

mysql> update (t1 as s3, t1 as t3) set t3.price_inc_vat=round(s3.price*1.3, 2) where s3.vat_rate=20 and t3.vat_rate=20; 

mysql> select * from (t1 as s3, t1 as t3) where s3.vat_rate=20 and t3.vat_rate=20; 
1 1.00 1.30 20 1 1.00 1.30 20 
2 2.00 1.30 20 1 1.00 1.30 20 
1 1.00 1.30 20 2 2.00 1.30 20 
2 2.00 1.30 20 2 2.00 1.30 20 

如果你正在尝试设置基于以前的价格设置,那么产品的价格也许下面将有助于澄清事情:

mysql> create table t1 (
    product_id integer unsigned, 
    vat_rate integer unsigned, 
    price decimal(10,2), 
    price_inc_vat decimal(10,2), 
    primary key(product_id, vat_rate) 
); 

mysql> insert into t1 (product_id, price, price_inc_vat, vat_rate) values (1, 1.00, 1.20, 20), (2, 2.00, 2.40, 20), (1, 1.00, 0, 30), (2, 2.00, 0, 30); 

mysql> create temporary table tmp1 like t1; 

mysql> insert into tmp1 select * from t1; 

mysql> select * from t1; 
1  20  1.00 1.20 
1  30  1.00 0.00 
2  20  2.00 2.40 
2  30  2.00 0.00 

mysql> select * from tmp1; 
1  20  1.00 1.20 
1  30  1.00 0.00 
2  20  2.00 2.40 
2  30  2.00 0.00 

mysql> update t1 left join tmp1 on t1.product_id=tmp1.product_id and t1.vat_rate > tmp1.vat_rate set t1.price_inc_vat = round(tmp1.price*(1+t1.vat_rate/100), 2) where tmp1.vat_rate = 20; 

mysql> select * from t1; 
1  20  1.00 1.20 
1  30  1.00 1.30 
2  20  2.00 2.40 
2  30  2.00 2.60 
+0

感谢您的快速答复。我使用代码根据同一表上不同行的值更新行,但对于大表,行是相同的。 –

+0

我不敢相信你真的想加入与更新自己的表。看短表结果,并请再次告诉如果这是你的预期结果http://sqlfiddle.com/#!2/70531/2/0 –

+0

@SirRufo你的评论是针对StephenBouffe还是我? – Cez