执行sql delete查询foreach user_id

时间:2017-09-26 13:02:21

标签: mysql sql

是否有可能在表中执行sql delete query foreach user_id? 我可以使用SELECT user_id FROM个用户获取所有user_id 然后我可以删除除

之外的所有原始内容
DELETE FROM accounts
WHERE id IN (select * from (SELECT id 
FROM   accounts 
WHERE  user_id = 150192618395339835 
       AND site = 'https://www.paypal.com' 
       AND time_added < (SELECT Max(time_added) 
                         FROM   accounts 
                         WHERE  user_id = 150192618395339835 
                                AND site = 'https://www.paypal.com') 
ORDER  BY time_added ASC) as t)

如何将所有user_id传递给删除查询?

1 个答案:

答案 0 :(得分:1)

尝试

WHERE user_id in (SELECT u.user_id FROM users u) 

所有查询:

DELETE FROM accounts
WHERE id IN (select * from (SELECT id 
FROM   accounts 
WHERE  user_id in (SELECT u.user_id FROM users u) 
       AND site = 'https://www.paypal.com' 
       AND time_added < (SELECT Max(time_added) 
                         FROM   accounts 
                         WHERE  user_id = 150192618395339835 
                                AND site = 'https://www.paypal.com') 
ORDER  BY time_added ASC) as t)