[英]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. 我要做的是删除lobby表中没有useronline表中的id的行。 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? (实际的脚本不是关于谁不在线但是它的逻辑相同)有没有办法我可以先从
useronline
选择id,然后在lobby
搜索那些不是我刚刚选择的那些,并删除他们有一个while循环?
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"] . "')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.