简体   繁体   中英

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. 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. How to also delete the clicks that are associated with such image via the post_id of both tables?

This is the event that is currently runnning, only deleting the images older than the 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. Dont tried in MySQL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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