简体   繁体   English

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

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

I have two tables: 我有两个表:

Images 图片

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 已点击

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

Whenever an image is clicked, with ajax, I am adding such click to the clicked table and relate it to the user that clicked the image and the post whose click came from. 每当使用ajax单击图像时,我都会将这种单击添加到clicked表中,并将其与单击该图像的用户以及单击其来源的帖子相关联。 This is just what the tables are used for. 这就是表的用途。

The real question is: 真正的问题是:

I delete pictures older than the CURRENT_TIMESTAMP of the server** using an event that runs on a minute-ly manner. 我使用每分钟运行一次的事件删除早于服务器CURRENT_TIMESTAMP **的图片。 How to also delete the clicks that are associated with such image via the post_id of both tables? 如何删除通过两个表的POST_ID与这样的图像相关的点击

This is the event that is currently runnning, only deleting the images older than the CURRENT_TIMESTAMP: 这是当前正在运行的事件,仅删除早于CURRENT_TIMESTAMP的图像:

DELETE * FROM images WHERE post_time <= CURRENT_TIMESTAMP;

**the date of the posts are set to a future date **帖子的日期设置为将来的日期

Can someone lead me in the right way please? 有人可以用正确的方式带领我吗? That would be appreciated. 这将不胜感激。 Thank you. 谢谢。

    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

It is true for MS SQL. 对于MS SQL是正确的。 Dont tried in MySQL 不要在MySQL中尝试

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

相关问题 选择一个表中的每一行以及另一表中的相关行 - select every row in one table and related rows from another 如何获取与另一个表中的多行相关的行? - How to fetch a row which is related to multiple rows in another table? 从一个表中提取多行,但从相关表中仅提取一行 - Pull multiple rows from one table but only one row from a related table 当另一个表中没有行时,如何从一个表中删除行 - How do I delete a row from one table, when no more rows exist in another 如何将一个表中的行合并到另一表中的另一行 - How to merge rows from one table to another row in another table 如何从一个表中选择单行和从另一个表中选择多行? - how to select single row from one table and multiple rows from another table toghether? 如何将多行作为一行插入到Mysql中的另一张表 - How to Insert multiple rows as one row to another table in Mysql 将一行连接到另一个表中的多行 - Join one row to multiple rows in another table 如何从一个表中选择一行,并从另一个(相关)表中选择许多行 - How to select a row from a table and many rows from another (related) table 如何从一个表中获取所有产品,并从包含多行的另一张表中合并一行? - How to fetch all the products from one table and merge one row from another table which contain multiple rows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM