[英]How to delete one row from a table and multiple rows on another that are related?
I have two tables: 我有两个表:
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
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.