SQL 如何在 Excel 中进行内部联接(例如,使用 VLOOKUP)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35164745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:23:16  来源:igfitidea点击:

How to inner-join in Excel (eg. using VLOOKUP)

sqlexcelinner-joinvlookup

提问by Wabbage

Is there a way to inner join two different Excel spreadsheets using VLOOKUP?

有没有办法使用 VLOOKUP 内部连接两个不同的 Excel 电子表格?

In SQL, I would do it this way:

在 SQL 中,我会这样做:

SELECT id, name
FROM Sheet1
INNER JOIN Sheet2
ON Sheet1.id = Sheet2.id;

Sheet1:

表 1:

+----+------+
| ID | Name |
+----+------+
|  1 | A    |
|  2 | B    |
|  3 | C    |
|  4 | D    |
+----+------+

Sheet2:

表2:

+----+-----+
| ID | Age |
+----+-----+
|  1 |  20 |
|  2 |  21 |
|  4 |  22 |
+----+-----+

And the result would be:

结果将是:

+----+------+
| ID | Name |
+----+------+
|  1 | A    |
|  2 | B    |
|  4 | D    |
+----+------+

How can I do this in VLOOKUP? Or is there a better way to do this besides VLOOKUP?

我怎样才能在 VLOOKUP 中做到这一点?或者除了 VLOOKUP 之外还有更好的方法吗?

Thanks.

谢谢。

采纳答案by Scott Craner

First lets get a list of values that exist in both tables. If you are using excel 2010 or later then in Sheet 3 A2 put the following formula:

首先让我们获取两个表中存在的值列表。如果您使用的是 excel 2010 或更高版本,则在 Sheet 3 A2 中输入以下公式:

=IFERROR(AGGREGATE(15,6,Sheet2!$A:$A00/(COUNTIF(Sheet1!$A:$A00,Sheet2!$A:$A00)>0),ROW(1:1)),"")

If you are using 2007 or earlier then use this array formula:

如果您使用的是 2007 或更早版本,请使用以下数组公式:

=IFERROR(SMALL(IF(COUNTIF(Sheet1!$A:$A00,Sheet2!$A:$A00),Sheet2!$A:$A00),ROW(1:1)),"")

Being an array formula, copy and paste into the formula bar then hit Ctrl-Shift-Enter instead of Enter or Tab to leave the edit mode.

作为数组公式,复制并粘贴到公式栏中,然后按 Ctrl-Shift-Enter 而不是 Enter 或 Tab 以退出编辑模式。

Then copy down as many rows as desired. This will create a list of ID'd that are in both lists. This does assume that ID is a number and not text.

然后根据需要复制尽可能多的行。这将创建一个包含在两个列表中的 ID 列表。这确实假设 ID 是数字而不是文本。

Then with that list we use vlookup:

然后使用该列表,我们使用 vlookup:

=IF(A2<>"",VLOOKUP(A2,Sheet1!A:B,2,FALSE),"")

This will then return the value from Sheet 1 that matches.

这将返回匹配的 Sheet 1 中的值。

enter image description here

在此处输入图片说明

回答by Seb

You can acheive this result using Microsoft Query.

您可以使用 Microsoft Query 获得此结果。

First, select Data > From other sources > From Microsoft Query

首先,选择 Data > From other sources > From Microsoft Query

enter image description here

在此处输入图片说明

Then select "Excel Files*".

然后选择“Excel 文件*”。

In the "Select Workbook" windows, you have to select the current Workbook.

在“选择工作簿”窗口中,您必须选择当前工作簿。

Next, in the query Wizard windows, select sheet1$ and sheet2$ and click the ">" button. enter image description here

接下来,在查询向导窗口中,选择 sheet1$ 和 sheet2$ 并单击“>”按钮。 在此处输入图片说明

Click Next and the query visual editor will open.

单击下一步,查询可视化编辑器将打开。

Click on the SQL button and paste this query :

单击 SQL 按钮并粘贴此查询:

SELECT `Sheet1$`.ID, `Sheet1$`.Name, `Sheet2$`.Age
FROM`Sheet1$`, `Sheet2$`
WHERE `Sheet1$`.ID = `Sheet2$`.ID

Finally close the editor and put the table where you need it.

最后关闭编辑器并将表格放在您需要的地方。

The result should look like this : enter image description here

结果应该是这样的: 在此处输入图片说明

回答by App Review

For Basic Excel Join without formuales or Excel Macros. Please check the website http://exceljoins.blogspot.com/2013/10/excel-inner-join.html

对于没有公式或 Excel 宏的基本 Excel 联接。请查看网站 http://exceljoins.blogspot.com/2013/10/excel-inner-join.html

Joins can Left Outer, Right Outer and Full Outer which used in rare ocassions, But we can achieve this for Excel Sheets, For more information check the below http://exceljoins.blogspot.com/

连接可以在极少数情况下使用的左外、右外和全外,但我们可以为 Excel 表格实现这一点,有关更多信息,请查看以下 http://exceljoins.blogspot.com/