Jump to content

[SOLVED] delete where id != array[id]


webent

Recommended Posts

Hi, I was wondering if someone could help my brain past this task... I need to create this clean up script that deletes categories that do not have correlating products... but of course, doing something like this "delete where id != array[id]" definately does not work, as you can imagine, the first id it gets, it deletes them all...

 

It almost needs to compare the entire array at once before any deletion is made... because doing it in any kind of order, deletes everything else before it even gets a chance to compare. Here's my code...

 

$query = "SELECT DISTINCT master_categories_id FROM products";
$results = mysql_query($query);
while ($row  =  mysql_fetch_array($results))	{
$sub_query = mysql_query("DELETE FROM categories WHERE categories_id != '$row[master_categories_id]'");
$sub_query = mysql_query("DELETE FROM categories_description WHERE categories_id != '$row[master_categories_id]'");
						  						}

Link to comment
Share on other sites

select categories_id FROM categories as c 
LEFT JOIN products as p ON ( c.category_id = p.master_categories_id ) 
WHERE p.master_categories_id IS NULL

 

Will give you the "empty" categories... it's not difficult to convert this into a delete.

Link to comment
Share on other sites

Here, does this look good?

 

$query = mysql_query("SELECT categories_id FROM categories as c 
LEFT JOIN products as p ON ( c.category_id = p.master_categories_id ) 
WHERE p.master_categories_id IS NULL");
while ($row  =  mysql_fetch_array($query))	{
$sub_query = mysql_query("DELETE FROM categories WHERE categories_id = '$row[categories_id]'");
$sub_query = mysql_query("DELETE FROM categories_description WHERE categories_id = '$row[categories_id]'");
										}

 

Or did I go about it wrong?

Link to comment
Share on other sites

Yeah, I must have gone about it wrong... got this error...

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/username/public_html/data_uploader/database_management.php on line 53

Invalid query: Unknown column 'c.category_id' in 'on clause' Whole query:

Link to comment
Share on other sites

Yes, you were right... slap myself... I didn't check it properly...

 

LEFT JOIN products as p ON ( c.category_id = p.master_categories_id )

 

should be...

 

LEFT JOIN products as p ON ( c.categories_id = p.master_categories_id )

 

Thank you very much for your help.

Link to comment
Share on other sites

Here's an odd one... so I took that code and made it to do the manufacturers also... the thing with the manufacturers table though, is that the id in that table and the product table are the same... so I got an error... "Column 'manufacturers_id' in field list is ambiguous" So I look it up, the solution it says to use is to prepend the field with the table... like so...

 

$query = mysql_query("SELECT manufacturers.manufacturers_id FROM manufacturers as c 
LEFT JOIN products as p ON ( c.manufacturers_id = p.manufacturers_id ) 
WHERE p.manufacturers_id IS NULL");

 

Then I get the error... "Unknown table 'manufacturers' in field list" Which is bogus because that table does exist, spelled exactly that way...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.