简体   繁体   English

首先从一个表中选择id然后删除那些不在第二个表中的ID

[英]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.

相关问题 如何从两个表中选择数据,而两个表只为第一表的一行选择了第二表的第一行 - How to SELECT data from two tables which only first row of second table is selected for one row of first table MySQL CodeIgniter是否基于第二个表的第一个Select列进行选择 - MySQL CodeIgniter Select or not from second table based on column of first Select 从一个表中选择行,其中基于第二个表ID的另一个第二个表中的ID显示结果的顶部以及下一个结果,因为它是+ Mysql - Select rows from a table where id in another second table based on second table ids shows results top as well as next results as it is + Mysql 从一个MySQL表的两行获取ID,然后使用PHP在另一个MySQL表的一行中输入这些值 - Taking ids from two rows of one MySQL table and entering those values in one row of another MySQL table using PHP 将表链接到第二个表并将数据从第一个表多次拉到第二个表 - Linking a table to a second table and pulling data from the first one to the second one multiple times 从与第一条记录匹配的表中选择第二条记录 - Select second record from table matching with first record 选择仅限于具有第二张表的特定值的记录 - Select records limited to those with specific value of a second table 从两个表中选择,从第二个表中按一行排序 - select from two tables ordering by one row from second table 子查询:根据匹配的用户ID和团队ID从一张表中进行选择,并输出二维数组 - subqueries: select from one table based on matching user ids with team ids and output 2 dimensional array laravel一对多(从第二个表中选择所有行) - laravel one to many(select all rows from second table)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM