E
E
Evgeny Shevtsov2015-12-17 19:51:10
MySQL
Evgeny Shevtsov, 2015-12-17 19:51:10

How to rewrite sql overgrown insert to update?

There is a dump.
The format is like this..

INSERT INTO `product_images` (`product_id`, `image`) VALUES
(103, 'shops/1/products/95fd9653b27e9d745ed42c3f553be3c53d2c22d3.png'),
(104, 'shops/1/products/7c195d10be6dc71be339d151225573cf251103f1.png'),
(105, 'shops/1/products/a5c854d236f4b9478dd37a3619b02f060264da80.png'),
(106, 'shops/1/products/2712489054f2704f47cef92722b256846af01602.png'),
(107, 'shops/1/products/5090d07f7377e67b737095868a223807bf425069.png'),
(108, 'shops/1/products/36c4d68cf3a6d2244db4f99429e5ef1d3e7efbf4.png'),
(109, 'shops/1/products/ef6a83b637a57636cae2558fcfa640213b9f7d56.png'),
(110, 'shops/1/p.... и т.д около 2 000 столбцов

How can I change this query to update the image column where product_id = first parameter (103, 104,105) ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
romy4, 2015-12-17
@Rattlesneyk

insert into product_images_tmp, and then update product_images inner join product_images_tmp
ps by the way, in your query there is a mismatch in the number of columns

I
Ivanq, 2015-12-17
@Ivanq

UPDATE `product_images` SET image = 'shops/1/products/95fd9653b27e9d745ed42c3f553be3c53d2c22d3.png' WHERE product_id = 103
UPDATE `product_images` SET image = 'shops/1/products/95fd9653b27e9d745ed42c3f553be3fd34fd343.png' WHERE product_id = 104
UPDATE `product_images` SET image = 'shops/1/products/95fd9653b27e9d745ed42c3f553b434dfd3rd3e4d3.png' WHERE product_id = 105

etc

R
Rsa97, 2015-12-17
@Rsa97

If product_id is UNIQUE or PRIMARY KEY then append to the end of the query
ON DUPLICATE KEY UPDATE `image` = VALUE(`image`)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question