Forum Moderators: open

Message Too Old, No Replies

Mysql, deleting vinculated records

I can get this sql sentence to work.

         

asantos

2:01 am on May 19, 2006 (gmt 0)

10+ Year Member



Hi, i have 3 tables:
* content
* content_data
* content_file

content has an ID called: id_section
content_data has an ID called: id_content
content_file has an ID called: id_content_data

1) I need to DELETE all the records from content_file that are vinculated to the records from content where id_section=5

2) I need to DELETE all the records from content_data that are vinculated to the records from content where id_section=5

Can (1) or (2) be executed from one SQL sentence?
So far I have this SQL sentence but it only selects the records (doesn't delete them):

SELECT content_file.id_content_data FROM content INNER JOIN (content_data INNER JOIN content_file ON content_data.id_content_data = content_file.id_content_data) ON content.id_content = content_data.id_content
WHERE (((content.id_section)=5));

Any ideas? Thanks!

asantos

2:20 am on May 19, 2006 (gmt 0)

10+ Year Member



I was thinking in something like this, but it may take a lot of resources:

$sql = 'SELECT id_content FROM content WHERE id_section=5';
$rsContent = &$cnn->Execute($sql);
while(!$rsContent->EOF) {
$sql = 'SELECT id_content_data FROM content_data WHERE id_content='.q($rsContent->fields['id_content']);
$rsContentData = &$cnn->Execute($sql);
while(!$rsContentData->EOF) {
$sql = 'SELECT id_content_file FROM content_file WHERE id_content_data='.q($rsContentData->fields['id_content_data']);
$rsContentFile = &$cnn->Execute($sql);
while(!$rsContentFile->EOF) {
$cnn->Execute('DELETE FROM content_file WHERE id_content_file='.q($rsContentFile->fields['id_content_file']));
$rsContentFile->MoveNext();
}
$cnn->Execute('DELETE FROM content_data WHERE id_content_data='.q($rsContentData->fields['id_content_data']));
$rsContentData->MoveNext();
}
$rsContent->MoveNext();
}
$cnn->Execute('DELETE FROM content WHERE id_section=5';

coopster

6:18 pm on May 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can perform multiple table DELETE [dev.mysql.com] functions starting with MySQL >= 4.0, depending on the syntax. As far as which is faster, two statements versus a single statement using a JOIN, will likely depend on your INDEXes, etc. You would have to setup a test and time the two to tell the difference.