比较不同表中的2个Mysql列并删除

时间:2012-04-11 21:46:14

标签: php mysql compare

好的,所以我的问题是这个

我有一个支持自动销售的拍卖网站,但是当拍卖被自动播放时它不会清理旧条目,所以我打算编写一些PHP代码并将其作为cronjob运行以清除条目后说大约2天左右,被引用的表格是probid_auctions& probid_auction_media和该列是auction_id,我想把probid_auction_media中的auction_id列与probid_auctions中的auction_id列进行比较,并删除probid_auction_media中与之匹配的任何条目

立即测试

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");

2 个答案:

答案 0 :(得分:1)

DELETE FROM probid_auction_media 
WHERE auction_id NOT IN (SELECT DISTINCT auction_id FROM probid_auctions);

答案 1 :(得分:1)

使用像这样的查询

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);