Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从另一个表中的内容为真的表中删除_Mysql - Fatal编程技术网

Mysql 从另一个表中的内容为真的表中删除

Mysql 从另一个表中的内容为真的表中删除,mysql,Mysql,我有两个表:用户表和禁止表。我想从bans中删除该行中的用户ID在30天或更长时间内未处于活动状态的任何行。在我的users表中,有一列名为last_session,另一列作为时间戳保存为其上次连接的整数;在我的bans表中,有一列名为Banked_user_id,它只是被禁止用户的id。请尝试以下操作 Delete b.* from bans b, users u where b.banned_user_id = u.ID and u.last_session < subdate(n

我有两个表:用户表和禁止表。我想从bans中删除该行中的用户ID在30天或更长时间内未处于活动状态的任何行。在我的users表中,有一列名为last_session,另一列作为时间戳保存为其上次连接的整数;在我的bans表中,有一列名为Banked_user_id,它只是被禁止用户的id。

请尝试以下操作

Delete b.* from bans b, users u 
where b.banned_user_id = u.ID 
and u.last_session < subdate(now(),30)

普拉宾答案的一个变体。我更喜欢使用连接

delete b
from bans b
inner join users u on
  b.banned_user_id = u.id 
where 
  u.last_session < subdate(now(),30)

此选项仅从bans表中删除记录,如问题中所述:

delete from bans
where banned_user_id in (
  select user_id
  from users
  where last_session <= subdate(now(), 30)
)