DIGIT-ARC read-only archive · snapshot: July 23, 2026

MySQL Query - Can anyone help?

3 posts · originally from https://geek.digit.in/community/threads/mysql-query-can-anyone-help.107133/

DizitalNovice
DizitalNovice Original post #1
Jan 22, 2009 at 1:55 PM
This seems so simple yet I just cannot get it!

The context:
I am building a library mgmt system and there is a table called requests where current requests for book id are kept as (buk_id,usr_id,Date of rqst).
Now everytime an entry is made here number of available copies in the books table decreases by 1. The requests are valid for 2 days after which entry is deleted from requests table and available copies is increased by 1.
I used query
Code:
update books b set available_copies=available_copies+1 where b.book_id in( SELECT book_id FROM `lib_request` l WHERE datediff(curdate(),l.date)>3)
The problem:
Whenever the same book exists more than once in the invalid requests query ie.
Code:
SELECT book_id FROM `lib_request` l WHERE datediff(curdate(),l.date)>3
returns multiple rows with same book_id. available copies increases only by 1...

I hope you understand the problm. can you help?
victor_rambo
Jan 22, 2009 at 4:23 PM
Code:
UPDATE books SET available_copies=available_copies+(SELECT COUNT(*) FROM lib_request WHERE lib_request.book_id=books.book_id AND datediff(curdate(),l.date)>3)
This should work!
DizitalNovice
Jan 23, 2009 at 2:42 PM
Let me try and tell you
I think it does work...
Thank you...
but can you explain this query...