A
A
ademar262017-09-21 10:14:54
MySQL
ademar26, 2017-09-21 10:14:54

How to remove rows from tables selected by select?

I need to delete rows from tables, I write a query

DELETE FROM ( SELECT * FROM `oc_product` p LEFT JOIN oc_product_description p2s ON (p.product_id = p2s.product_id) LEFT JOIN oc_product_to_category p3s ON (p.product_id = p3s.product_id) LEFT JOIN oc_category_description p4s ON (p3s.category_id = p4s.category_id) LEFT JOIN oc_product_to_store p5s ON (p.product_id = p5s.product_id) LEFT JOIN oc_product_to_layout p6s ON (p.product_id = p6s.product_id) WHERE p.manufacturer_id = '30' )

gives an error message. If it's just select, then everything is selected.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-09-21
@qonand

DELETE
FROM
  `oc_product`
WHERE
  id IN (
    SELECT
      p.product_id
    FROM
      `oc_product` p
    LEFT JOIN oc_product_description p2s ON (
      p.product_id = p2s.product_id
    )
    LEFT JOIN oc_product_to_category p3s ON (
      p.product_id = p3s.product_id
    )
    LEFT JOIN oc_category_description p4s ON (
      p3s.category_id = p4s.category_id
    )
    LEFT JOIN oc_product_to_store p5s ON (
      p.product_id = p5s.product_id
    )
    LEFT JOIN oc_product_to_layout p6s ON (
      p.product_id = p6s.product_id
    )
    WHERE
      p.manufacturer_id = '30'
  )

R
Rsa97, 2017-09-21
@Rsa97

Considering only LEFT JOIN is used
To remove records from related tables, you need to make the relationship foreign keys (FOREIGN KEY) with the attribute ON DELETE CASCADE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question