在查询中实现子查询,该查询使用max(dateOut)返回DVD的上一次借出。
该查询在借贷表中给出了正确的回报,但是当尝试实现为子查询时会出现错误,您知道应该怎么做? max(dateout)部分。测试时出现如下错误。
SELECT
DVDTitle.DVDTitle,
Loan.LoanNumber,
Member.MemberNumber,
Member.MemberFirstName,
Member.MemberLastName,
(SELECT max(DateOut) FROM Loan, DVDCopy GROUP BY DVDCopy.CopyNumber),
Loan.DateDue, Loan.DateReturned
FROM
DVDCopy
INNER JOIN
Loan
ON DVDCopy.CopyNumber = Loan.CopyNumber
INNER JOIN
Member
ON Loan.MemberNumber = Member.MemberNumber
INNER JOIN
DVDTitle
ON DVDCopy.DVDNumber = DVDTitle.DVDNumber
WHERE
(DVDCopy.CopyNumber = @CopyNumber)
错误是:
执行查询时出错。请检查命令的语法以及参数的类型和值(如果存在),并确保它们正确无误。子查询返回的值超过1。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
预期结果应基于下拉列表从网格表中的借出表和DVD复制表中基于借出号返回特定DVD的最后借出(此处选择了借出号)。任何想法,谢谢。
答案 0 :(得分:0)
如何将SELECT
中的子查询移动到INNER JOIN
:
SELECT
DVDTitle.DVDTitle,
Loan.LoanNumber,
Member.MemberNumber,
Member.MemberFirstName,
Member.MemberLastName,
mdo.MaxDateOut,
Loan.DateDue, Loan.DateReturned
FROM
DVDCopy
INNER JOIN
Loan
ON DVDCopy.CopyNumber = Loan.CopyNumber
INNER JOIN
Member
ON Loan.MemberNumber = Member.MemberNumber
INNER JOIN
DVDTitle
ON DVDCopy.DVDNumber = DVDTitle.DVDNumber
INNER JOIN
(SELECT CopyNumber,max(DateOut) as "MaxDateOut" FROM Loan, DVDCopy GROUP BY DVDCopy.CopyNumber) mdo
ON DVDCopy.CopyNumber = mdo.CopyNumber
WHERE
(DVDCopy.CopyNumber = @CopyNumber)