Reputation: 1189
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.
Upvotes: 0
Views: 43
Reputation: 36
It looks like you should be able to join the loan table and borrower table and use a WHERE clause. Try this:
SELECT
*
FROM borrower B
JOIN loan L
ON L.Borrower_idBorrowerPK=B.idBorrowerPK
where datediff(L.dateReturned,L.dateBorrowed)>31
Upvotes: 1
Reputation: 10807
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
Upvotes: 1