I have a library database where users can borrow media from my inventory list. I am trying to query the database to find out the last user to borrow certain media. Rather than get the last user(s) to borrow the media I get a list of all the users to make a borrow transaction. I believe I am just leaving out something very simple. Please help.
This is my query:
SELECT Users.LastName,Users.UserID,InvTransactions.TransDate FROM InventoryData,InvTransactions,TransToUser,Users WHERE Users.UserID=TransToUser.UserID AND TransToUser.TransactID=InvTransactions.TransactID AND InventoryData.InventoryID=24
Jan MontanoPosted Dec 29, 2008, 10:31 PM
I'm assuming there's only one BOOK1. Sam won't be able to loan BOOK1 unless it is returned by john.
Although you submitted his second transaction after you loaned sam book1, he must not be included in the result because your condition specifically states BOOK1 with latest transaction date.
you gave another book to john but it's not BOOK1.
I'm also assuming that with InventoryData.InventoryID=24, it filters records with BOOK1.
also, do you have some kind of transactiontype with values either 'Borrowed' or 'Returned?' If you have none, think of including it in your table.
MariaPosted Dec 29, 2008, 10:52 AM
Jan MontanoPosted Dec 29, 2008, 12:52 AM
SELECT Users.LastName,Users.UserID,InvTransactions.TransDate FROM InventoryData,InvTransactions,TransToUser,Users WHERE Users.UserID=TransToUser.UserID AND TransToUser.TransactID=InvTransactions.TransactID AND InventoryData.InventoryID=24
AND InvTransactions.TransDate =
(SELECT MAX(InvTransactions.TransDate WHERE Users.UserID=TransToUser.UserID AND TransToUser.TransactID=InvTransactions.TransactID AND InventoryData.InventoryID=24)
--or you could use the top keyword instead
SELECT top 1 Users.LastName,Users.UserID,InvTransactions.TransDate FROM InventoryData,InvTransactions,TransToUser,Users WHERE Users.UserID=TransToUser.UserID AND TransToUser.TransactID=InvTransactions.TransactID AND InventoryData.InventoryID=24
ORDER BY InvTransactions.TransaDate desc