Robert Ross Robert Ross - 1 month ago 7
MySQL Question

Comparing two datetime fields in a table and displaying a record in another table based on the result

I am trying to compare the to datetime fields in my

loan
table so if the difference between the dates is bigger then 31 days I can get the names from the
borrowers
table of the borrowers, who delayed the return of their book. I am not allowed to use triggers or stored procedures for the task, so I am aiming to do it in another way, but I don't even know where to start.
I'd appreciate any tips on how to do that.

Here is the part of my database that is used for the task.

enter image description here

Answer

This sentence should do the work:

SELECT
    borrower.idBorrowerPK,
    borrower.name
FROM
    loan
    left join borrower
        on load.Borrower_idBorrowerPK = borrower.idBorrowerPK
WHERE
    DATEDIFF(CURDATE(), loan.dateBorrowed) >= 31
Comments