Select if it does not exist in another table, but if there is no record in 24h

Asked

Viewed 300 times

0

I have two tables:

links inserir a descrição da imagem aqui

Works inserir a descrição da imagem aqui I need to return the values of the links table, but first I need to see if it exists in the Works table, and if it exists it must have been inserted more than 24h, otherwise this record cannot be returned in the query.

Consult and return only what I do not have in the Works table is easy, I do so:

SELECT *
FROM links l
WHERE NOT EXISTS 
(
    SELECT * 
    FROM works w
    WHERE w.linkId = l.linkId
) 

But I need to return only the records entered more than 24 hours

1 answer

1

You can add the check from datahora in his sub-select using the function DATE_SUB:

SELECT *
FROM links l
WHERE NOT EXISTS 
(
    SELECT * 
    FROM works w
    WHERE w.linkId = l.linkId
    AND w.datahora <= DATE_SUB(NOW(), INTERVAL 24 HOUR);
) 

See more about DATE_SUB here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.