【问题标题】:SQL - Keep only columns that have some field that don't matchSQL - 仅保留某些字段不匹配的列
【发布时间】:2021-10-05 12:48:41
【问题描述】:

我每天都在更新一个表,但我只想更新前一天修改过任何值的行。

例如,我有 2 张桌子(今天和昨天):

今天:

ID NAME MONTH COUNTRY YEAR
1 Sta April Jamaica 1992
2 Danny November France 1982
3 Elle June Australia 2020
4 John April Uruguay 1765
5 Paul May Canada 1990

昨天:

ID NAME MONTH COUNTRY YEAR
1 Sta April Jamaica 1992
2 Danny November Spain 1982
3 Elle July Australia 2022
5 Paul May Canada 1990

所以我想从 Today 创建一个只有 ID 2、3 和 4 的表,因为 1 个或多个字段发生了变化,或者该行出现在 Today 表中但它没有出现在 Yesterday 表中:

ID NAME MONTH COUNTRY YEAR
2 Danny November France 1982
3 Elle June Australia 2020
4 John April Uruguay 1765

我尝试使用 MINUS 功能,但我使用的是 MS-Access,但它不起作用。有 +100 列,所以我不能这样做: WHERE Today.ID = Yesterday.ID AND (Today.NAME Yesterday.NAME OR Today.COUNTRY Yesterday.COUNTRY OR Today.YEAR Yesterday.YEAR)

【问题讨论】:

    标签: sql ms-access


    【解决方案1】:

    你可以使用not exists:

    select t.*
    from today as t
    where not exists (select 1
                      from yesterday as y
                      where y.id = t.id and y.name = t.name and
                            y.month = t.month and y.country = t.country and
                            y.year = t.year
                     );
    

    【讨论】:

    • 是的,但我不能这样做,因为我有 +100 列,所以我不能写 100 AND。这是唯一的解决方案吗?
    • @Dylan 。 . .在 MS Access 中,您需要列出所有列。
    【解决方案2】:

    虽然这是使用 SQL 的噩梦,但使用 VBA 和一些循环变得相对容易。类似的东西似乎有效:

    Sub sCheckDifference()
        On Error GoTo E_Handle
        Dim db As DAO.Database
        Dim rsToday As DAO.Recordset
        Dim rsYesterday As DAO.Recordset
        Dim strSQL As String
        Dim strNoMatch As String
        Dim lngCount As Long
        Dim lngLoop1 As Long
        Set db = DBEngine(0)(0)
        db.Execute "DELETE * FROM tblNoMatch;"
        Set rsToday = db.OpenRecordset("SELECT * FROM tblToday;")
        If Not (rsToday.BOF And rsToday.EOF) Then
            lngCount = rsToday.Fields.Count - 1
            Do
                strSQL = "SELECT * FROM tblYesterday WHERE ID=" & rsToday!id
                Set rsYesterday = db.OpenRecordset(strSQL)
                If Not (rsYesterday.BOF And rsYesterday.EOF) Then
                    For lngLoop1 = 1 To lngCount
                        If rsToday(lngLoop1) <> rsYesterday(lngLoop1) Then
                            strNoMatch = strNoMatch & rsToday!id & ","
                            Exit For
                        End If
                    Next lngLoop1
                Else    ' value doesn't exist in previous table
                    strNoMatch = strNoMatch & rsToday!id & ","
                End If
                rsToday.MoveNext
            Loop Until rsToday.EOF
            If Len(strNoMatch) > 0 Then '   have found some differences
                If Right(strNoMatch, 1) = "," Then strNoMatch = Left(strNoMatch, Len(strNoMatch) - 1)
                strSQL = "INSERT INTO tblNoMatch SELECT * FROM tblToday WHERE ID IN(" & strNoMatch & ");"
                db.Execute strSQL
            End If
        End If
    sExit:
        rsToday.Close
        rsYesterday.Close
        Set rsToday = Nothing
        Set rsYesterday = Nothing
        Set db = Nothing
        Exit Sub
    E_Handle:
        MsgBox Err.Description & vbCrLf & vbCrLf & "sCheckDifference", vbOKOnly + vbCritical, "Error: " & Err.Number
        Resume sExit
    End Sub
    

    请注意,Name、Month 和 Year 都是 Access 中的保留字,因此如果这些是真实的字段名称,则应更改它们。

    问候,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      相关资源
      最近更新 更多