Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • Sign Up

sql запрос для количества товара = сумме опций


Recommended Posts

Помогите составить sql запрос, чтобы массово установить количество товара равным сумме всех опций.

 

Т.е  quantity в таблице oc_product ровно сумме quantity из oc_product_option_value

 

image.png.0247adab07d468ba37b5ac1b152a7f8f.png

oc_proc_productoduct

Link to post
Share on other sites

как-то так:

UPDATE oc_product AS pu
INNER JOIN (
SELECT temp.product_id, MAX(temp.qty_sum) AS qty FROM
(SELECT product_id, SUM(quantity) AS qty_sum
FROM oc_product_option_value GROUP BY product_option_id) AS temp
GROUP BY product_id) as q
ON q.product_id = pu.product_id
SET quantity = q.qty

 

Link to post
Share on other sites
16 минут назад, PaulKravchenko сказал:

как-то так:


UPDATE oc_product AS pu
INNER JOIN (
SELECT temp.product_id, MAX(temp.qty_sum) AS qty FROM
(SELECT product_id, SUM(quantity) AS qty_sum
FROM oc_product_option_value GROUP BY product_option_id) AS temp
GROUP BY product_id) as q
ON q.product_id = pu.product_id
SET quantity = q.qty

 

Отлично работает! Спасибо огромное.

Link to post
Share on other sites

4 часа назад, PaulKravchenko сказал:

как-то так:


UPDATE oc_product AS pu
INNER JOIN (
SELECT temp.product_id, MAX(temp.qty_sum) AS qty FROM
(SELECT product_id, SUM(quantity) AS qty_sum
FROM oc_product_option_value GROUP BY product_option_id) AS temp
GROUP BY product_id) as q
ON q.product_id = pu.product_id
SET quantity = q.qty

 

..будет излишне и неверно из за GROUP BY product_option_id - могут быть разными ;)

 

так правильней

UPDATE `oc_product` p
INNER JOIN (
SELECT product_id, SUM(quantity) AS qty_sum
FROM `oc_product_option_value`
GROUP BY product_id) pov ON p.product_id = pov.product_id SET p.quantity = pov.qty_sum;

 

  • +1 1
Link to post
Share on other sites

Только надо еще проверить есть ли у товара опции, а то если нет - 0 будет

Link to post
Share on other sites
41 минуту назад, spectre сказал:

Только надо еще проверить есть ли у товара опции, а то если нет - 0 будет

JOIN

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.