Sql “选择不存在/不存在的位置”子查询(VB.NET,Access)出现问题
我有个问题需要帮助。 代码如下:Sql “选择不存在/不存在的位置”子查询(VB.NET,Access)出现问题,sql,vb.net,subquery,Sql,Vb.net,Subquery,我有个问题需要帮助。 代码如下: Private Sub dtpStartDate_ValueChanged(sender As Object, e As EventArgs) Handles dtpStartDate.ValueChanged startDateChanged = 1 If endDateChanged = 1 Then cbLocation.Enabled = True cbLocation.Items.Clear()
Private Sub dtpStartDate_ValueChanged(sender As Object, e As EventArgs) Handles dtpStartDate.ValueChanged
startDateChanged = 1
If endDateChanged = 1 Then
cbLocation.Enabled = True
cbLocation.Items.Clear()
cbLocation.Items.Add(New ListViewItem(""))
Dim unbookedLocationsSQL As String = "SELECT locationID FROM Locations WHERE NOT EXISTS (Select LocationID FROM Bookings WHERE @startDate <= bookingEndDate AND bookingStartDate <= @endDate)"
Dim unbookedLocationsCommand = New OleDbCommand(unbookedLocationsSQL, globalVariables.objConnection)
Dim unbookedLocationsAdapter As New OleDbDataAdapter(unbookedLocationsSQL, globalVariables.objConnection)
Dim unbookedLocationsDataSet As New DataSet
unbookedLocationsCommand.Parameters.AddWithValue("startDate", dtpStartDate.Value)
unbookedLocationsCommand.Parameters.AddWithValue("endDate", dtpEndDate.Value)
unbookedLocationsAdapter.Fill(unbookedLocationsDataSet, "Locations")
With cbLocation
.DataSource = unbookedLocationsDataSet.Tables("Locations")
.DisplayMember = "locationID"
.ValueMember = "locationID"
End With
End If
End Sub
首先,如果将sql语句更改为从位置选择*,则组合框将显示所有位置
我想达到的是这一点;当有人更改两个日期时间选择器时,组合框将启用并填充在这两个日期之间未预订的位置列表,该列表由“预订”表确定。
我知道SQL语句是错误的。我尝试了各种各样的组合,并尝试了分离一些零碎的东西,但我无法得到任何子查询来实现我想要的
任何帮助都将不胜感激。我觉得这里有点不对劲
WHERE @startDate <= bookingEndDate AND bookingStartDate <= @endDate
如果我理解正确的话,Bookings表中的每个条目都反映了一次预订,即使是在特定地点。在这种情况下,您需要:
WHERE @startDate >= bookingEndDate OR bookingStartDate >= @endDate
而不是
WHERE @startDate <= bookingEndDate AND bookingStartDate <= @endDate
此外,如果Bookings表可以为同一位置包含多个条目,则这将不起作用:您必须确保该位置的所有预订都与输入日期不重叠
由于我还不能对其他答案发表评论,我将在这里注意到Codemunkee的答案是错误的-使用他的查询,其中bookingStartDate>=@startDate和bookingEndDate这里是代码,它现在正在工作:
Private Sub dtpStartDate_ValueChanged(sender As Object, e As EventArgs) Handles dtpStartDate.ValueChanged
startDateChanged = 1
If endDateChanged = 1 Then
cbLocation.Enabled = True
Me.Refresh()
cbLocation.Items.Add(New ListViewItem(""))
Dim unbookedLocationsSQL As String = "SELECT * FROM Locations WHERE LocationID NOT IN (SELECT LocationID FROM Bookings WHERE bookingEndDate >= @startDate AND bookingStartDate <= @endDate)"
Dim unbookedLocationsCommand = New OleDbCommand(unbookedLocationsSQL, globalVariables.objConnection)
Dim unbookedLocationsAdapter As New OleDbDataAdapter(unbookedLocationsSQL, globalVariables.objConnection)
Dim unbookedLocationsDataSet As New DataSet
unbookedLocationsCommand.Parameters.AddWithValue("startDate", dtpStartDate.Value)
unbookedLocationsCommand.Parameters.AddWithValue("endDate", dtpEndDate.Value)
unbookedLocationsAdapter.SelectCommand = unbookedLocationsCommand
unbookedLocationsAdapter.Fill(unbookedLocationsDataSet, "Locations")
With cbLocation
.DataSource = unbookedLocationsDataSet.Tables("Locations")
.DisplayMember = "LocationName"
.ValueMember = "LocationID"
End With
End If
End Sub
您是否尝试过隔离此查询?从@startDate所在的预订中选择LocationID
WHERE @startDate <= bookingEndDate AND bookingStartDate <= @endDate
Private Sub dtpStartDate_ValueChanged(sender As Object, e As EventArgs) Handles dtpStartDate.ValueChanged
startDateChanged = 1
If endDateChanged = 1 Then
cbLocation.Enabled = True
Me.Refresh()
cbLocation.Items.Add(New ListViewItem(""))
Dim unbookedLocationsSQL As String = "SELECT * FROM Locations WHERE LocationID NOT IN (SELECT LocationID FROM Bookings WHERE bookingEndDate >= @startDate AND bookingStartDate <= @endDate)"
Dim unbookedLocationsCommand = New OleDbCommand(unbookedLocationsSQL, globalVariables.objConnection)
Dim unbookedLocationsAdapter As New OleDbDataAdapter(unbookedLocationsSQL, globalVariables.objConnection)
Dim unbookedLocationsDataSet As New DataSet
unbookedLocationsCommand.Parameters.AddWithValue("startDate", dtpStartDate.Value)
unbookedLocationsCommand.Parameters.AddWithValue("endDate", dtpEndDate.Value)
unbookedLocationsAdapter.SelectCommand = unbookedLocationsCommand
unbookedLocationsAdapter.Fill(unbookedLocationsDataSet, "Locations")
With cbLocation
.DataSource = unbookedLocationsDataSet.Tables("Locations")
.DisplayMember = "LocationName"
.ValueMember = "LocationID"
End With
End If
End Sub