TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-03-2008, 03:16 AM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default 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
delayedinsanity is offline  
Reply With Quote
Old 04-03-2008, 06:53 AM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Shouldn't this be in MySql forum? Also I'll help:
Code:
DELETE FROM `table` WHERE `column` = `row`
Correct? or what? I mean if you have two of the same rows in two tables and they're both used as the same, then you should do the same query only delete it from that other table
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 04-03-2008, 01:08 PM   #3 (permalink)
The Wanderer
 
serversphere's Avatar
 
Join Date: Dec 2006
Location: USA
Posts: 21
Thanks: 0
serversphere is on a distinguished road
Default

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
LEFT JOIN contact_additional AS ca 
ON c.ticket=ca.ticket
WHERE c.ticket='MM1234.1234'
Send a message via AIM to serversphere
serversphere is offline  
Reply With Quote
Old 04-03-2008, 02:42 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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`;
and get...

Code:
ERROR 1054 (42S22): Unknown column 'mm1234.1234' in 'where clause'
So I'm not sure how to tell it what information to look for in the delete. I don't want it to just delete all columns that match up, that's just bad. The more I read the manual here, it looks like I may have to do a SELECT first...

Code:
CREATE TEMPORARY TABLE tmptable
SELECT t1.* FROM table1 as t1
LEFT JOIN table2 as t2 ON t2.id = t1.id
WHERE t2.id is NULL;

DELETE FROM table1 USING tmptable, table1
WHERE table.id = tmptable.id;
...anyways... Sorry about this being posted in the wrong forum, I've honestly never scrolled that far down before to realize there was one! I'm new (obviously) so I've just been catching up reading the beginner/advanced and general forums. If a mod wants to move it, I'll know where to find it! Thanks for all the help guys.
-m
delayedinsanity is offline  
Reply With Quote
Old 04-03-2008, 04:57 PM   #5 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
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`;
and get...

Code:
ERROR 1054 (42S22): Unknown column 'mm1234.1234' in 'where clause'
You put backticks around mm1234.1234 so it thinks that is a column and not a string. I think you want something like this.

sql Code:
DELETE FROM contact, contact_additional
USING contact, contact_additional
WHERE contact.ticket = contact_additional.ticket
AND contact.ticket = 'mm1234.1234';
That will delete where contact.ticket's value is mm1234.1234.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 04-03-2008, 05:39 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
USING contact, contact_additional
WHERE (contact.ticket=contact_additional.ticket AND contact.ticket='mm1234.1234')
OR contact.ticket='mm1234.1234';
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:59 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design