I have two tables called TEMPDATA and Product. I am dumping all my data from a csv feed into TEMPDATA and then organising it into the respective tables, Product being one of them.
I am trying to insert all the contents of TEMPDATA that does not have a duplicate 'ean' number into Product, providing it doesn't already exist in Product.
The query I am trying to use is below...
INSERT IGNORE INTO `Product` (`product_ean`, `product_name`, `product_description`, `product_image`, `product_thumbnail`, `product_category`)
SELECT `ean`, `product_name`, `description`, `merchant_image_url`, `aw_thumb_url`, `merchant_category`
FROM (
SELECT `ean`, `product_name`, `description`, `merchant_image_url`, `aw_thumb_url`, `merchant_category`, count( * )
FROM TEMPDATA
GROUP BY `ean`
HAVING count( * ) = 1
) AS t
WHERE NOT EXISTS (
SELECT `product_ean` FROM Product
)
All the individual sections seem to be working, except from when I include the 'WHERE NOT EXISTS' clause, could someone please help me out?
try this:
INSERT IGNORE INTO Product (product_ean, product_name, product_description, product_image, product_thumbnail, product_category)
SELECT ean, product_name, description, merchant_image_url, aw_thumb_url, merchant_category
FROM (
SELECT ean, product_name, description, merchant_image_url, aw_thumb_url, merchant_category, count( * )
FROM TEMPDATA
GROUP BY ean
HAVING count( * ) = 1
) AS t
WHERE NOT EXISTS (
SELECT product_ean FROM Product AS A WHERE A.product_ean = t.product_name
)
you were missing this:
WHERE A.product_ean = t.product_name
in the NOT EXIST statement
you need to make you include the the where clause to search, see below
INSERT IGNORE INTO `Product` (`product_ean`, `product_name`, `product_description`, `product_image`, `product_thumbnail`, `product_category`)
SELECT `ean`, `product_name`, `description`, `merchant_image_url`, `aw_thumb_url`, `merchant_category`
FROM (
SELECT `ean`, `product_name`, `description`, `merchant_image_url`, `aw_thumb_url`, `merchant_category`, count( * )
FROM TEMPDATA
GROUP BY `ean`
HAVING count( * ) = 1
) AS t
WHERE NOT EXISTS (
SELECT `product_ean` FROM Product P where t.ean = p.`product_ean`
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With