简体   繁体   中英

First select ids from one table then delete the ones that aren't those in the second table

What i'm trying to do is delete the rows in lobby table that don't have the ids in useronline table. that way i will be able to eliminate the ones who aren't "online". (the actual script is not about who's not online but its the same logic) Is there a way that I can first select the ids from useronline , then search in lobby for the ones that aren't those i've just selected, and delete them with a while loop?

This is my non-working script to show you what i've got for the idea so far:

$sql = mysql_query("SELECT DISTINCT `id` FROM `useronline` WHERE 1");
while($row = mysql_fetch_array( $sql )) {
mysql_query("DELETE * 
FROM  `lobby` 
WHERE  `tableid` NOT IN ('$row') <-- Can't figure out how to make this part
LIMIT 0 , 30");
}

You can do this with one query.

DELETE FROM  `lobby` 
WHERE  `tableid` NOT IN (SELECT DISTINCT `id` FROM `useronline`)

为了使代码保持原样,您希望将$ row更改为$ row [“id”],如下所示:

WHERE  `tableid` NOT IN ('" . $row["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