繁体   English   中英

如何从表中删除一行,而在另一表上删除多行相关?

[英]How to delete one row from a table and multiple rows on another that are related?

我有两个表:

图片

post_id     user_id     post_text     post_time
----------------------------------------------------------
1           1           TEXT          2014-01-12 11:07:00
2           2           TEXT          2014-01-12 12:10:00

已点击

clicked_id        post_id        user_id 
-----------------------------------------
1                 1              1       
2                 1              2       
3                 2              1        

每当使用ajax单击图像时,我都会将这种单击添加到clicked表中,并将其与单击该图像的用户以及单击其来源的帖子相关联。 这就是表的用途。

真正的问题是:

我使用每分钟运行一次的事件删除早于服务器CURRENT_TIMESTAMP **的图片。 如何删除通过两个表的POST_ID与这样的图像相关的点击

这是当前正在运行的事件,仅删除早于CURRENT_TIMESTAMP的图像:

DELETE * FROM images WHERE post_time <= CURRENT_TIMESTAMP;

**帖子的日期设置为将来的日期

有人可以用正确的方式带领我吗? 这将不胜感激。 谢谢。

    declare @CurTime datetime = CURRENT_TIMESTAMP

    delete from C
    from Images I 
    join Clicked C
    on C.post_ID = I.post_ID
    where I.post_time <= @CurTime

    delete from Images 
    where post_time <= @CurTime

对于MS SQL是正确的。 不要在MySQL中尝试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM