简体   繁体   English

如何在子查询上进行简单联接?

[英]How can I do a simple join on a subquery?

I've been at this for 2 hours and have no idea what the issue is. 我已经呆了两个小时了,不知道问题是什么。 Although I have worked a fair amount with SQL, I'm struggling with the idiosyncrasies in Access queries relative to SQL queries when using the ADO ACE connection to query Excel worksheets. 尽管我在SQL方面做了很多工作,但是在使用ADO ACE连接来查询Excel工作表时,我还是在Access查询中相对于SQL查询的特质中苦苦挣扎。 My goal at it's most basic is to do a join with a subquery. 我最基本的目标是使用子查询进行联接。 I have simplified the query considerably to display the issue, so please excuse the fact that it wouldn't really make sense to run a query like this. 我已经大大简化了查询以显示问题,所以请原谅这样的事实实际上并不可行。

The error I keep getting is Syntax Error in From Clause . 我不断收到的Syntax Error in From Clause The worksheet only has 2 columns, Account Number and Cost 工作表只有两列, Account NumberCost

Sub stackoverflow()

Dim acctcon As New ADODB.Connection
Dim acctrec As New ADODB.Recordset

acctcon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\name\Documents\Book1.xlsx;" & _
                            "Extended Properties=" & Chr(34) & "Excel 12.0 Xml;HDR=YES" & Chr(34) & ";"

acctcon.Open

querystr = "Select [Accounts$].[Account Number], [mm].[Cost] " & _
        "FROM [Accounts$] " & _
        "JOIN (Select [Account Number], [Cost] " & _
            "FROM [Accounts$]) As mm " & _
        "ON [Accounts$].[Account Number] = [mm].[Account Number]"

acctrec.Open querystr, acctcon

'Syntax Error on From Clause

End Sub

Try to reset the Close bracket behind As mm 尝试将关闭支架重置为As mm

querystr = "Select [Accounts$].[Account Number], [mm].[Cost] " & _
           "FROM [Accounts$] " & _
           "INNER JOIN (Select [Account Number], [Cost] " & _
           "FROM [Accounts$] As mm) " & _
           "ON [Accounts$].[Account Number] = [mm].[Account Number]"

But you have to replace the inner join, because it makes no sense like @krish KM stated. 但是您必须替换内部联接,因为它就像@krish KM所说的一样没有意义。 I suppose you took it as a placeholder. 我想你把它当作一个占位符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM