delete from Rentals
where (select datediff(day, OrderDate, actualReturnDate)
from Orders
inner join rentals on Orders.orderNumber = Rentals.orderNumber) > 60
我遇到错误
子查询返回了多个值
答案 0 :(得分:1)
查询中的子查询返回多个值,如果将它们与单个值进行比较则不允许这样做。
我怀疑您只是在以错误的方式编写删除语句。您要实现的目标很薄弱。
我会猜测你打算写什么:
delete Rentals
from Orders
INNER JOIN rentals ON Orders.orderNumber=Rentals.orderNumber
where DATEDIFF(day, OrderDate, actualReturnDate)>60
这将删除rentals
和orderdate
之间的差,其中与租赁相关的订单的行大于{60}。
请注意,由于您在编写像这样的琐碎查询时遇到问题,因此最好先读一本好书或任何可以教您编写T-SQL查询基础知识的书籍。
答案 1 :(得分:0)
您可以尝试以下操作:
delete from Rentals
where Rentals.ID in (select rentals.ID
from Orders
inner join rentals on Orders.orderNumber = Rentals.orderNumber
datediff(day, OrderDate, actualReturnDate) > 60)
在“ WHERE”之后,Sql比较字段中的值(在这种情况下,字段=列),而不是值的集合
答案 2 :(得分:0)
该部分查询无效:
Option Explicit
Sub CopySheets()
Dim path As String
Dim FileName As String
Dim whichSheet As String
Dim SheetNames As String
Dim wb As Workbook
path = GetFolder
If path = vbNullString Then
MsgBox "No folder was selected. Ending the procedure."
End
End If
FileName = Dir(path & "*.xlsx")
whichSheet = InputBox("Which month would you like to copy? Enter month (eg. Jan, Feb, Mar)")
Do While FileName <> ""
Set wb = Workbooks.Open(FileName:=path & FileName, ReadOnly:=True)
On Error Resume Next
If Len(wb.Sheets(whichSheet).Name) = 0 Then 'Here we handle an error on the inputname for the sheet.
On Error GoTo 0
SheetNames = GetSheetNames(wb)
MsgBox "The input sheet does not exist in this workbook. The current worksheet names are: " & SheetNames
whichSheet = InputBox("Which month would you like to copy? Enter month (eg. Jan, Feb, Mar)")
End If
With wb.Sheets(whichSheet)
.Copy after:=ThisWorkbook.Sheets(1)
.Close
End With
ThisWorkbook.Sheets(2).Name = Left(FileName, Application.WorksheetFunction.Search(" ", FileName) - 1)
FileName = Dir()
Loop
End Sub
Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Selecciona una carpeta"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
Function GetSheetNames(wb As Workbook) As String
Dim ws As Worksheet
For Each ws In wb.Worksheets
GetSheetNames = GetSheetNames & ", " & ws.Name
Next ws
End Function
那是因为select语句返回多个行,并且SQL引擎无法识别应该将datediff与60进行比较,即两者都比较?只有第一个? in或in和...?尚不清楚,这就是SQL引擎抱怨的原因。您可以在子查询中包含过滤器,也可以将查询重写为TT。做到了。