请在下面的表中找到我想要执行中间和内部联接SQL查询的内容。
表1: -
表2: -
请查看我的以下语法以及我所面临的相应错误。
第一个SQL查询语法
sql_string = "SELECT [Sheet1$].[Sr], [Sheet2$].[Value], [Name] FROM" & _
" [Sheet2$] INNER JOIN [Sheet1$] ON" & _
" [Sheet1$].[Sr]=(SELECT mid([Sheet2$].[Name], INSTR([Sheet2$].[Name],""#""), len([Sheet2$].[Name])) FROM [Sheet2$]);"
上面代码出现错误。
第二个SQL查询语法
sql_string = "SELECT [Sheet1$].[Sr], [Sheet2$].[Value], [Name] FROM " & _
"(SELECT mid([Sheet2$].[Name], INSTR([Sheet2$].[Name],""#""), len([Sheet2$].[Name]))KeyValue FROM [Sheet2$]) as [Sheet2$]" & _
" [Sheet2$] INNER JOIN [Sheet1$] ON [Sheet1$].[Sr]=[Sheet2$].KeyValue;"
上面代码出现错误。
请指导我。
请找到预期的输出。
答案 0 :(得分:0)
使用以下查询
Select [Sheet1$].[Sr], [Sheet2$].[Name], [Sheet2$].[Value] From [Sheet1$] Inner Join [Sheet2$] On CInt([Sheet1$].[Sr]) = CInt(Mid([Sheet2$].[Name],InStr(1,[Sheet2$].[Name],""#"")+1))
示例代码
Dim objCN, objRS
Set objCN = CreateObject("ADODB.Connection")
Set objRS = CreateObject("ADODB.RecordSet")
On Error Resume Next
objCN.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\pankaj.jaju\Desktop\Book1.xlsx';Extended Properties='Excel 12.0;HDR=YES;IMEX=1';"
objRS.Open "Select [Sheet1$].[Sr], [Sheet2$].[Name], [Sheet2$].[Value] From [Sheet1$] Inner Join [Sheet2$] On CInt([Sheet1$].[Sr]) = CInt(Mid([Sheet2$].[Name],InStr(1,[Sheet2$].[Name],""#"")+1))", objCN, 3, 1, 1
If Err.Number <> 0 Then
MsgBox Err.Number & ": " & Err.Description
Else
On Error GoTo 0
Sheet3.Range("A1").CopyFromRecordset objRS
End If
objRS.Close: Set objRS = Nothing: Set objCN = Nothing