Morning, I am trying to create a count of products per category / store using an UPDATE, JOIN, SUBQUERY but I cant get the syntax right, can someone help me where I am going wrong:
UPDATE
oc_category_to_store c2s
SET
c2s.product_count =
(
SELECT
count(p.product_id) as product_count,
p2c.category_id,
p2s.store_id
FROM
oc_product p
INNER JOIN oc_product_to_category p2c ON
(p.product_id = p2c.product_id)
INNER JOIN oc_product_to_store p2s ON
(p.product_id = p2s.product_id)
) AS pc
WHERE
(p2c.category_id = pc.category_id) AND
(p2s.store_id = pc.store_id)
Essentially this is in OpenCart but the existing product count routines are done in PHP are on large DBs of products it slows the site down to a crawl.
products are linked to both categories and stores by ID using the product_to_category and product_to_store tables, categories and also linked to stores using a category_to_store table.
I want to run a mySQL query to update the product counts per category / store by storing the value in the category_to_store table as this only needs to run when products are added / deleted / updated and not every time any page loads.
CREATE TABLE `oc_product_to_category` (
`product_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
PRIMARY KEY (`product_id`,`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AVG_ROW_LENGTH=9 ROW_FORMAT=FIXED;
CREATE TABLE `oc_product_to_store` (
`product_id` int(11) NOT NULL,
`store_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`product_id`,`store_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AVG_ROW_LENGTH=9 ROW_FORMAT=FIXED;
CREATE TABLE `oc_category_to_store` (
`category_id` int(11) NOT NULL,
`store_id` int(11) NOT NULL,
`product_count` int(11) DEFAULT NULL,
PRIMARY KEY (`category_id`,`store_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AVG_ROW_LENGTH=9 ROW_FORMAT=FIXED;
You need to use the INNER JOIN
syntax:
UPDATE
oc_category_to_store a
INNER JOIN
(
SELECT
b.category_id,
c.store_id,
COUNT(*) AS productcnt
FROM
oc_product a
INNER JOIN
oc_product_to_category b ON a.product_id = b.product_id
INNER JOIN
oc_product_to_store c ON a.product_id = c.product_id
GROUP BY
b.category_id,
c.store_id
) b ON
a.category_id = b.category_id AND
a.store_id = b.store_id
SET
a.product_count = b.productcnt
The 1st error is in your query logic. Your subquery returns 3 columns:
product_count,
p2c.category_id,
p2s.store_id
And you're trying to assign this triple to a single column *c2s.product_count*
UPDATE
The 2nd error is in the WHERE clause of your main query. You use tables p2c , p2s and pc however UPDATE clause utilizes c2s only. The most likely the WHERE clause must be in the subquery like below:
UPDATE
oc_category_to_store c2s
SET
c2s.product_count =
(
SELECT
count(oc_product.product_id) as product_count,
FROM
oc_product p
INNER JOIN oc_product_to_category p2c ON
(p.product_id = p2c.product_id)
INNER JOIN oc_product_to_store p2s ON
(p.product_id = p2s.product_id)
WHERE
(p2c.category_id = pc.category_id) AND
(p2s.store_id = pc.store_id)
)
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.