基本上,我有一个 Excel 工作表,其中包含大约 5k 多个社会安全号码的列表,我想将他们的电子邮件地址添加到 Excel 表中。
是否可以设置电子表格来运行简单的 SQL 查询,例如:
SELECT emailaddress
FROM table
WHERE ssn = '?'
除了让它根据查询所在的行自动更改变量,以及让它自动沿着电子表格运行,只需使用相应的 ssn 进行查询?
谢谢!
----------------------------------------编辑------ -------------------------------------------------------
我想如果这有助于澄清的话,下面是我的 Excel 工作表的基本示例:
A B C D E
1 | Name | Address | Phone Number | Social | |
2 | Brian | 123 Abc ST. | 555-1234 | 111-22-3333 | |
3 | John | 88 N. Road | 444-4848 | 123-45-4568 | |
Etc...
我们的数据库有这个,还有更多字段,所以我想包含他们的电子邮件,并将其添加到表中。
A B C D E
1 | Name | Address | Phone Number | Social | Email |
2 | Brian | 123 Abc ST. | 555-1234 | 111-22-3333 | brian@gmail.com |
3 | John | 88 N. Road | 444-4848 | 123-45-4568 | john@hotmail.com |
^Add this column^
基本上只需将相同的查询放入电子邮件列中,并让它引用其各自的 SSN 单元格即可。因此,相同的查询将进入 Brian 和 John 的电子邮件单元格,但变量将自动选择同一行中的社交。
Query:
SELECT emailaddress
FROM table <-------------Use this query in this column--|
WHERE ssn = '?' But have the variable change |
depending on the row it is in V
A B C D E
1 | Name | Address | Phone Number | Social | Email |
2 | Brian | 123 Abc ST. | 555-1234 | 111-22-3333 | "...where ssn = (D2)"|
3 | John | 88 N. Road | 444-4848 | 123-45-4568 | "...where ssn = (D3)"|
^Query Example^
希望能够澄清事情。
最佳答案
我认为您能做的最好的事情就是针对只读数据库构造多个查询,例如:
select [your_desired_fields]
from table
where SSN in (ssn-1, ssn-2, ... ssn-N);
MySQL 的 IN
子句可接受的逗号分隔参数数量(例如,Oracle 将其上限为 1,000)。然后手动或通过查询的 UNION 将结果拼凑在一起。
我使用 Oracle,但我认为如果你想联合它们,你可以这样做:
select [your_desired_fields]
from table
where SSN in (ssn-1, ssn-2, ... ssn-1000)
UNION
select [your_desired_fields]
from table
where SSN in (ssn-1001, ssn-2, ... ssn-2000)
UNION
etc, etc.
此处的 Range2Csv()
VBA 函数可以帮助构建该查询:
http://dullsharpness.com/2011/11/14/excel-vba-range-to-csv-range2csv-function/
关于mysql - 在Excel中多次执行SQL查询并根据行更改变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32653758/