0

I tried something like this, but it does only delete the categories so far.

DELETE s, i 
FROM imgbox_categories s 
INNER JOIN imgbox_images i 
ON s.category_id = i.fk_category_id 
WHERE category_id = @id

Additionaly I get an error

Incorrect syntax near ','.
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
Harugawa
  • 539
  • 5
  • 19

3 Answers3

1

Since you didn't tag any DBMS involved, you can use EXISTS() which is ANSI SQL:

DELETE from imgbox_categories 
WHERE EXISTS(SELECT 1 FROM imgbox_images  i
             WHERE imgbox_categories.category_id = i.fk_category_id )
 AND imgbox_categories.category_id = @id
sagi
  • 40,026
  • 6
  • 59
  • 84
0

Your query seems to be correct, check this answer.

You are trying to delete from two tables at the same time, I think you cannot do this. Here is the comma where you get the error: DELETE s, i

If you delete from imgbox_categories with your query and then from imgbox_images using a simple query, it should work.

Community
  • 1
  • 1
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
0

If you want delete from 2 tables, you can use 2 SQL queries:

DELETE FROM imgbox_categories WHERE category_id = @id;
DELETE FROM imgbox_images WHERE fk_category_id = @id;
Kaspy
  • 54
  • 1
  • 10