I'm trying to delete all records form the table "oc_products" that don't have a certain category ID. I created a SELECT query that lists those products using an INNER JOIN, since the categories are in a separate table.
What I can't figure out is how to use the DELETE function to delete the shown records.
This is what my code looks like:
DELETE oc_product
FROM oc_product
INNER JOIN oc_product_to_category ON oc_product.product_id = oc_product_to_category.product_id
WHERE oc_product_to_category.category_id = 343
Its showing the error "Unexpected keyword, (near INNER JOIN)".
Add .* to p in your first line.
Try:
DELETE p.* FROM oc_product p
INNER JOIN oc_product_to_category pc ON p.product_id =
pc.product_id
WHERE pc.category_id = 343
Related
I have four tables products, product_histories, vendor_invoices and invoices
This is the query I have developed
SELECT p.product_id, product_name, vendor_name FROM products AS p
INNER JOIN product_histories AS ph ON p.product_id = ph.product_id
CASE
WHEN ph.history_type = "P" THEN
LEFT JOIN vendor_invoices AS vi ON link_id = vi.vi_id
WHEN ph.history_type = "S" THEN
LEFT JOIN invoices AS i ON i.invoice_id = link_id
END
ORDER BY ph_id ASC
What I want that if ph.history_type is P then is should join vendor_invoices and if it is S then it should join invoices. But it says there is a syntax error.
Can anyone help me out with it? Or could show a better way to achieve this problem.
In my other question I asked how to select a column from multiple tables with inner joins. My new question is: how to delete these results?
SELECT
product_image.image
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type"
If you want to delete only from products then the below should do the job
delete p from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"
But if you need to delete from all the tables matching the joining condition then use
delete p,ipm,pi from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"
You can use this query
DELETE FROM product_image WHERE product_image_id IN (SELECT
product_image.image
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type")
EDIT : From the manual
Currently, you cannot delete from a table and select from the same
table in a subquery.
If you want to modify the same query you can execute it by creating a temporary table (here its resultset)
DELETE FROM product_image WHERE product_image_id IN ( SELECT resultset.product_image_id FROM (SELECT
product_image.product_image_id
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type") AS resultset )
OR you can use the USING like this in the example from the MySQL Manual,
13.2.2 DELETE Syntax. I haven't used the USING, but you can definetely check out.
DELETE FROM t1, t2 USING t1
INNER JOIN t2
INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Also this SO post will help too MySQL Error 1093 - Can't specify target table for update in FROM clause
I want to update a column in table INVENTAR from a couple of INNER JOINS.
I have the following:
Table INVENTAR with the column PRODUCT_ID,CATEGORY
Table PRODUCT_TO_CATEGORY with the columns PRODUCT_ID,CATEGORY_ID
Table CATEGORY_DESCRIPTION with the columns CATEGORY_ID, NAME
I want the NAME column to update the CATEGORY column.
Here's my code:
UPDATE inventar
SET inventar.category=category_description.name
FROM inventar
INNER JOIN product_to_category
ON product_to_category.product_id=inventar.product_id
INNER JOIN category_description
ON category_description.category_id=product_to_category.category_id
The correct MySQL syntax is:
UPDATE inventar i INNER JOIN
product_to_category ptc
ON ptc.product_id = i.product_id INNER JOIN
category_description cd
ON cd.category_id = ptc.category_id
SET i.category = cd.name;
Your syntax looks more appropriate for SQL Server or Postgres.
I'm trying to use this query
UPDATE products
SET products.product_price = '87.00000'
FROM products
INNER JOIN product_category
ON products.product_id = product_category.product_id
WHERE product_category.category_id = '64'
However I receive this error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM table products INNER JOIN table product_category ON prod' at line 3
I don't see any syntax error. I have made this query from examples on this forum.
remove the FROM products line.
and put the
SET ... line just before the WHERE clause.
to be clear :
UPDATE ...
JOIN ...
SET ...
WHERE ...
you could also do
UPDATE products p
SET p.product_price='87.00000'
WHERE EXISTS (SELECT NULL
FROM product_category pc
WHERE p.product_id = pc.product_id
AND pc.category_id = '64');
You can use the following:
UPDATE products
INNER JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64';
See SQL Fiddle with Demo
UPDATE products
INNER JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64'
I think the SET clause needs to be after the table references and before the WHERE clause, like this:
UPDATE products
INNER
JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64'
Here's how I get that syntax when I need an UPDATE like that. I always start with a select statement, and that lets me know which rows are going to be updated. For example:
SELECT p.*, c.*
FROM products p
JOIN product_category c
ON p.product_id = c.product_id
WHERE c.category_id = '64'
To convert that to an update statement, I add a "SET" clause after the table references and before the WHERE clause, and then replace "SELECT ... FROM" with the "UPDATE" keyword. Voila.
I have three tables, product, category and product_to_category. Product has the primary key product_id, category category_id and product_to_category p2c_id. Product_to_ category links products to categories in a many-to-many relationship using their respective ID's.
Basically I want to write a query that would select all products from categories that do not exist in the category table. This is due to products being migrated across from another database.
I had something like this but was a little lost.
SELECT *
FROM product AS p
LEFT JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
LEFT JOIN category AS c ON c.category_id
Basically that is as far as I have got. I need to join the category table to the product_to_category table where the product_to_category category_id is not in the category table. I may be completely on the wrong path but am stuck!
Thanks in advance.
Assumption: A product can be part of categories that exist, categories that do not exist, or no categories at all. You have not asked for products that belong to no categories at all, so the first LEFT JOIN from product to procduct_to_category should be an INNER JOIN.
Caveat: I am rusty at mysql so I am using SQL SERVER syntax. I forget if mysql has ON clauses or uses where clauses for JOINs. If ON clause is not supported, change them into WHERE clauses.
There are two common approaches: OUTER JOIN or a NOT IN clause (or a NOT EXISTS clause, which often behaves the same performance-wise as the NOT IN clause.)
OUTER JOIN
select p.*, p2c.category_id
from product p
INNER JOIN product_to_category p2c ON (p.product_id = p2c.product_id)
LEFT JOIN category c ON p2c.category_id = c.category_id
WHERE c.category_id IS NULL
The test for null will find the unmatched records.
NOT IN clause
SELECT p.*, p2c.category_id
FROM product p
INNER JOIN product_to_category p2c ON (p.product_id = p2c.product_id)
WHERE p2c.category_id NOT IN (SELECT c.category_id FROM category c)
If you're looking for products from nonexistent categories, I'd suggest
Select p.*,p2c.category_id
from product p
join product_to_category p2c
on p.product_id=p2c.product_id
left outer join category c
on p2c.category_id=c.category_id
where c.category_id is null
SELECT p.*
FROM product AS p
LEFT JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
WHERE NOT EXISTS (
SELECT 1
FROM category c
WHERE c.category_id = p2c.category_id
)
SELECT *
FROM product AS p
JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
JOIN category AS c ON c.category_id != as.category.
Try this?