简体   繁体   中英

Compare 2 Mysql Columns in different tables and delete

ok, so my problem is this

I have an auction website that supports autolisting, however what happens is when the auction gets autolisted it does not clean the old entries, so i was going to write some php code and run it as an cronjob to clear the entries say after 2 days or so, the tables being referenced are probid_auctions & probid_auction_media and the column is auction_id, I would like to take the column auction_id in probid_auction_media and compare it with the column auction_id in probid_auctions and delete any entries in probid_auction_media that did not match

testing this now

mysql_connect($host, $username, $password);
mysql_select_db("b1396hos_database1") or die( "Unable to select database");
$db->query("DELETE * FROM  probid_auction_media 
WHERE auction_id NOT IN (SELECT DISTINCT auction_id FROM probid_auctions");
DELETE FROM probid_auction_media 
WHERE auction_id NOT IN (SELECT DISTINCT auction_id FROM probid_auctions);

Use query like this

DELETE
FROM probid_auction_media
WHERE auction_id NOT IN(SELECT
                      probid_auction_media.auction_id
                    FROM probid_auctions,
                      probid_auction_media
                    where probid_auctions.auction_id != probid_auction_media.auction_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM