Excel等效的SQL查询" SELECT A.one,B.two FROM A INNER JOIN B ON A.three = B.three"

时间:2017-07-05 19:51:18

标签: mysql sql excel excel-vba excel-formula vba

你如何做SQL"选择加入"在excel? 假设我想要表A中的第一列,表B中的第二列,它们都有第三列。

我愿意:

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

但是我在Excel表格中有几个标签,而不是表格。如何在Excel中执行上述等效查询?

2 个答案:

答案 0 :(得分:2)

您可以使用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 Data Objects 2.8 Library”的引用(转到工具> VBA编辑器中的引用来执行此操作)。您将更改声明和初始化各种ADO对象的以下行:

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

Dim rs As ADODB.Recordset

Dim fld As ADODB.Field

答案 1 :(得分:1)

假设您的表格如下:

<强> Sheet 1中

enter image description here

<强> Sheet 2中

enter image description here

Cell A2的{​​{1}}中输入公式

Sheet3

并在=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)),"") 的{​​{1}}中输入以下公式

Cell B2

以上公式都是数组公式,因此按 Ctrl + Shift + Enter 进行提交。根据需要拖动/复制。见图片以供参考。

enter image description here

<强> -------------------------------------------- -------------------------------------------------- -------------------------

如果您还要在Sheet3(我的样本表中为=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)),"") )中显示前两页的第三列,请在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)),"")

根据需要拖动/复制。见下图。

enter image description here

来自@ ScottCraner的回答here

如果不使用公式和VBA,还有另一种方法可以实现这一目标。看看this是否有帮助。