簡體   English   中英

Excel等同於SQL查詢“從A.three = B.three的A內聯接B中選擇A.one,B.two”

[英]Excel Equivalent of SQL query “SELECT A.one, B.two FROM A INNER JOIN B ON A.three = B.three”

您如何在Excel中執行SQL“選擇加入”? 假設我想要表A的一欄,表B的兩欄,而他們兩個都有三欄。

我會做:

SELECT A.one, B.two
FROM A
INNER JOIN B ON
A.three = B.three

但是除了表格,我在Excel工作表中還有一些選項卡。 我該如何在Excel中執行上述查詢?

您可以使用VBA和ADO直接通過SQL查詢工作表。 只需將以下代碼添加到VBA模塊中的Sub中即可。 除了rs.Open行和With Worksheets行之外,該代碼主要是樣板,您應該對其進行修改以適合您的特定情況。

' Set up connection
Dim cn As Object
Set cn = CreateObject("ADODB.Connection")

' Connection string for Excel 2007 onwards .xlsm files
With cn
   .Provider = "Microsoft.ACE.OLEDB.12.0"
   .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
        "Extended Properties=""Excel 12.0 Macro;IMEX=1"";"
    .Open
End With

' Connection string for Excel 97-2003 .xls files
' It should also work with Excel 2007 onwards worksheets
' as long as they have less than 65536 rows
'With cn
'    .Provider = "Microsoft.Jet.OLEDB.4.0"
'    .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
'        "Extended Properties=""Excel 8.0;IMEX=1"";"
'    .Open
'End With

' Create and run the query
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")

' Join the two worksheets - assumed that these are named "Sheet1"
' and "Sheet2", the columns are named "one", "two" and "three"
' and that the results should be output to "Sheet3"
rs.Open "SELECT [Sheet1$].[one], [Sheet2$].[two] " & _
    "FROM [Sheet1$] INNER JOIN [Sheet2$] " & _
    "ON [Sheet1$].[three] = [Sheet2$].[three];", cn

' Output the field names and the results
Dim fld As Object
Dim i As Integer

' Change the worksheet to whichever one you want to output to
With Worksheets("Sheet3")
    .UsedRange.ClearContents

    For Each fld In rs.Fields
        i = i + 1
        .Cells(1, i).Value = fld.Name
    Next fld

    .Cells(2, 1).CopyFromRecordset rs
End With

' Tidy up
rs.Close
cn.Close

通過添加對“ Microsoft ActiveX數據對象2.8庫”的引用,可以將代碼更改為使用早期綁定(執行此操作,請轉到VBA編輯器中的“工具”>“引用”)。 您將更改以下聲明和初始化各種ADO對象的行:

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

Dim rs As ADODB.Recordset

Dim fld As ADODB.Field

假設您的工作表如下:

工作表Sheet1

在此處輸入圖片說明

Sheet2中

在此處輸入圖片說明

Sheet3 Cell A2中輸入公式

=IFERROR(INDEX(Sheet1!$A$2:$A$8,MATCH(SMALL(IF(COUNTIF(Sheet1!$C$2:$C$10,Sheet2!$C$2:$C$10),Sheet2!$C$2:$C$10),ROW(1:1)),Sheet1!$C$2:$C$8,0)),"")

Sheet3 Cell B2中輸入以下公式

=IFERROR(INDEX(Sheet2!$B$2:$B$8,MATCH(SMALL(IF(COUNTIF(Sheet1!$C$2:$C$10,Sheet2!$C$2:$C$10),Sheet2!$C$2:$C$10),ROW(1:1)),Sheet1!$C$2:$C$8,0)),"")

以上兩個公式都是數組公式,因此請按Ctrl + Shift + Enter提交。 根據需要向下拖動/復制。 參見圖片以供參考。

在此處輸入圖片說明

-------------------------------------------------- -------------------------------------------------- -------------------

如果您還想在Sheet3顯示前兩張紙的第三列(這是我的樣本紙中的ID ),請在Cell A2輸入以下公式

=IFERROR(SMALL(IF(COUNTIF(Sheet1!$C$2:$C$10,Sheet2!$C$2:$C$10),Sheet2!$C$2:$C$10),ROW(1:1)),"")

這也是一個數組公式。 Cell B2輸入

=IFERROR(INDEX(Sheet1!$A$2:$A$8,MATCH(A2,Sheet1!$C$2:$C$8,0)),"")

然后在Cell C2輸入

=IFERROR(INDEX(Sheet2!$B$2:$B$8,MATCH(A2,Sheet2!$C$2:$C$8,0)),"")

根據需要向下拖動/復制。 參見下圖。

在此處輸入圖片說明

從@ScottCraner的答案這里得到了

還有一種無需使用公式和VBA即可實現此目的的方法。 看看有助於。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM