-1

I'm have 2 queries which I want to combine into one:

DELETE from #__gbl_ads where id in ($adid) AND user_id=$userid

and

DELETE from #__gbl_wishlist where ad_id in ($adid) AND userid=$userid

What I'm trying to write is:

DELETE from #__gbl_ads where id in ($adid) AND user_id=$userid UNION DELETE from #__gbl_wishlist where ad_id in ($adid) AND userid=$userid

but it doesn't seem to work... Could somebody help me please?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Alexandru Vlas
  • 1,355
  • 3
  • 18
  • 30
  • You need [*this answer*](http://stackoverflow.com/a/4839931/767881) on SO. – Ravinder Reddy Mar 16 '14 at 19:11
  • Hi. thank you for the link, here is the code I tried but no success, did I write it correctly? `DELETE id, ad_id FROM #__gbl_ads INNER JOIN #__gbl_wishlist WHERE id in ($adid) AND ad_id in ($adid)` – Alexandru Vlas Mar 17 '14 at 16:27
  • also tried `DELETE id, ad_id FROM #__gbl_ads INNER JOIN #__gbl_wishlist WHERE id=$adid AND ad_id=$adid` – Alexandru Vlas Mar 17 '14 at 16:27

3 Answers3

1
DELETE from #__gbl_ads where id in ($adid) AND user_id=$userid;
DELETE from #__gbl_wishlist where ad_id in ($adid) AND userid=$userid;

use semi colons ";" after each query.

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
Chamil
  • 805
  • 5
  • 12
  • I have an if statement saying: `if (! $db->query ()) { throw new DataException ( JText::_ ( "NO_DELETE1" ), 400 );` – Alexandru Vlas Mar 16 '14 at 18:34
  • @AlexandruVlas: what is the error message from the query? Also try to run this in your own sql environment and check whether that gives an error. – Patrick Hofman Mar 16 '14 at 19:41
0

I don't think you can use UNION in delete statements. UNION is supposed to be used with SELECT statements only

A Nice Guy
  • 2,676
  • 4
  • 30
  • 54
0

You cannot combine two delete statements on different tables. You could use a where ... or on the same table, but that's not the case here.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325