MySQL Version: 5.6.27
I am using the following two tables
TABLE: oc_product
product_id int(11)
model varchar(64)
tax_class_id int(11)
and
TABLE: oc_product_to_category
product_id int(11)
category_id int(11)
I want to change the tax_class_id
for a few products on TABLE oc_product
. So I am running a SELECT
query before UPDATE
SELECT `product_id`, `model`, `tax_class_id`
FROM `oc_product`
WHERE
NOT `tax_class_id` = 12
AND
`product_id`
NOT IN ( SELECT `product_id`
FROM `oc_product_to_category`
WHERE `category_id`
IN ( 73, 78, 116, 119, 120, 121, 122, 123, 125, 126, 127, 143, 159, 170, 176, 183, 211, 212, 213, 250, 260, 389, 433)
)
Unfortunately the results are not as expected.
All I want is to select all product_id
from the table oc_product
where tax_class_id
is not 12 and product_id
does not exist in the results of the following sub query.
SELECT `product_id`
FROM `oc_product_to_category`
WHERE `category_id`
IN ( 73, 78, 116, 119, 120, 121, 122, 123, 125, 126, 127, 143, 159, 170, 176, 183, 211, 212, 213, 250, 260, 389, 433)
Did I miss something? Please advise!
Best Answer
Your unexpected results may be stemming from
product_id
being assigned to multiple categories in theoc_product_to_category
table. If a product is in e.g. category 1 AND category 73, it will still be returned by the inner query and thus excluded from the outer query.Produce a simple set of test data
(I probably have the syntax of the INSERT query backwards;) )
I believe your current query will return
You probably want something like
This should return
3 is excluded because it only exists in other groups; 4 and 5 are excluded because of the tax code
Changing the last line to
AllProductsBelongingToTheseExcluded.product_id IS NULL
(and the appropriate subquery alias) should return only the(3, '12', 8)
datapoint