![]() |
MySQL syntax for table join on delete
I'm having trouble with the syntax for this statement. When I delete a row from my contact table, I need to also delete any corresponding rows from my contact_additional table. Instead of doing two queries I was trying to do a join based on the syntax I'm reading in the manual, but it's not working;
DELETE FROM contact, contact_additional WHERE contact.ticket=`MM1234.1234` AND contact_additional.ticket=`MM1234.1234` I keep getting errors! I tried a bunch of variations but I can't figure it out yet. Also, I'm wondering if I should bother trying to do a join here, because if there's no data in the contact_additional table that matches, I'm guessing it won't delete either row? -m |
Shouldn't this be in MySql forum? Also I'll help:
Code:
DELETE FROM `table` WHERE `column` = `row` |
No idea if this will work (not sure you can actually run a join query against a table that is being edited) but try this:
Code:
DELETE FROM contact AS c, ca |
I think I'm just going to stick with two queries, because I need it to delete from the first table regardless of there being additional information in the second, but I want to figure this out just because I can't!
I tried yours serversphere and I got an error 1064 (42000) from MySQL. So I tried this, based off an example in the MySQL documentation at MySQL :: MySQL 5.0 Reference Manual :: 12.2.1 DELETE Syntax, but... Code:
DELETE FROM contact, contact_additional USING contact, contact_additional WHERE contact.ticket=contact_additional.ticket AND contact.ticket=`mm1234.1234`;Code:
ERROR 1054 (42S22): Unknown column 'mm1234.1234' in 'where clause'Code:
CREATE TEMPORARY TABLE tmptable-m |
Quote:
sql Code:
|
Now I feel silly. Thanks Eric, that worked. I tried this query based on that to try and delete rows from each table, or just a row from the contact table if no match was found in the additional table, and I wound up deleting ALL the records in the additional table (argh!);
Code:
DELETE FROM contact, contact_additional |
| All times are GMT. The time now is 02:22 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0