简体   繁体   中英

Update Table Contents With Value From The Same Table

I have a simple table with 10000 rows

product_id | price | price_inc_vat | vat_rate

I want to be able to update the value of price_inc_vat based on the value of price

I have tried this

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;

It doesn't error but seems to be taking a very long time as I had to kill the query after 1/2 hour.

Why the join?

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

UPDATED I'm not quite sure what you are trying to achieve by updating a product's "Inc. price" with another products price, but the reason that your query is taking so long is because of the way that you are joining.

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

If you are trying to set product prices based upon a previous price set then perhaps the following will help clarify things:

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

So you have a field for price and another for price_inc_vat and yet another for vat_rate ? Why not just store price and vat_rate and calculate price_inc_vat when it's needed?

Also, why are you multiplying by 1.3 when the VAT you're targeting appears to be 20%?

All these logical inconsistencies aside, couldn't you just do the following?

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

Or to update everything after a VAT change, disregarding the 10% markup:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM