Jump to content

[SOLVED] Delete table1.row inner join != table2.row


quasiman

Recommended Posts

I have two tables with similar data, their sku numbers being the primary similarity.  In one table, I have extra rows that I don't need, and I'd like to delete them.  So below I have my script...but instead of deleting only the rows that don't match, it deletes all records.

Also, the script keeps running continuously so I think it's in an endless loop somehow.

 

Any help would be greatly appreciated!

<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
include ('dbconnect.php');
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($dbase, $linkID) or die("Could not find database.");

$sql = "DELETE bad_rows.*
       FROM zdata_prodfile
        AS good_rows
       INNER JOIN jos_vm_product
        AS bad_rows
          ON (bad_rows.product_sku != good_rows.SKU)";

$query = mysql_query($sql, $linkID) or die(mysql_error());
printf("Records deleted: %d\n", mysql_affected_rows());
mysql_close($linkID) or die(mysql_error());
?>

Link to comment
Share on other sites

Run this first to see which will be deleted:

SELECT * FROM zdata_prodfile gr WHERE gr.sku NOT IN (SELECT * FROM jos_vm_product br WHERE br.product_sku);

 

Then execute:

DELETE FROM zdata_prodfile gr WHERE gr.sku NOT IN (SELECT * FROM jos_vm_product br WHERE br.product_sku);

Link to comment
Share on other sites

Never mind, I rewrote a little and it works  :D

$sql = "SELECT br.product_sku 
		FROM jos_vm_product br 
		WHERE br.product_sku 
		NOT IN 
			(SELECT gr.SKU FROM zdata_prodfile gr)";

I think I can figure out the rest from here.

 

Thanks! :)

Link to comment
Share on other sites

Never mind, I rewrote a little and it works  :D

$sql = "SELECT br.product_sku 
		FROM jos_vm_product br 
		WHERE br.product_sku 
		NOT IN 
			(SELECT gr.SKU FROM zdata_prodfile gr)";

I think I can figure out the rest from here.

 

Thanks! :)

 

Oeps my bad forgot that the subquery could only return one value, sorry ;)

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.