SQL UNION错误 - 所选列与

时间:2016-11-09 14:51:59

标签: vb.net datagridview inner-join union

我尝试使用SQL UNION查询来填充DataGridView

我试图加入的3个表格是

  • 客户 - 包含基本客户信息(PK: Customer_Code

  • Customer_Addresses - 包含联系信息(电话,传真,地址等)(PK: Cust_Code

  • Delivery_Addresses - 与Customer_Addresses相同,但适用于投放地点(PK: Cust_Code

我试图在DataGridView中显示每张桌子的一些数据。我希望能够在每一行上显示每个表中的数据,并使用上面提到的主键加入它。

到目前为止,我已尝试使用INNER JOINUNION JOIN进行加入。但是,INNER JOIN不允许我只从两个表中选择一些列,而UNION JOIN给了我以下错误;

  

联合查询的两个选定表或查询中的列数不匹配。

这是我的代码,我做错了什么?

ElseIf type = "Customer" Then
 sql = "SELECT [Customer_Code], [Customer_Name], [Customer_Category], " & _
       "[Average_Payment_Terms], [Notes], [Aq_Date], [salesRep], [hoEmail], [webpage] " & _
       "FROM Customers " & _
       "UNION SELECT [Contact_Code], [Contact_Type], [Contact_Name], [Add1], [Add2], " & _
       "[Add3], [Add4], [Add5], [Postcode], [Country], [Telephone], [Fax], " & _
       "[Email], [Mobile_Phone] FROM [Customer_Addresses]"

 Dim da As New OleDbDataAdapter(sql, con)
 Dim ds As New DataSet
 Dim dt As New DataTable

 da.Fill(ds) ' This is the line that the error occurs on
 dt = ds.Tables(0).Copy()

EDT

我已尝试再次使用JOIN并使用以下内容;

ElseIf type = "Customers" Then
   sql = "SELECT Customer_Addresses.Cust_Code, Customers.Customer_Name, " & _
          "Customers.Customer_Category, Customers.Average_Payment_Terms, Customers.Notes, " & _
          "Customers.Aq_Date, Customers.salesRep, Customers.hoEmail, Customers.webpage, " & _
          "Customer_Addresses.Contact_Code, Customer_Addresses.Contact_Name, " & _
          "Customer_Addresses.Contact_Type, Customer_Addresses.Add1, Customer_Addresses.Add2, " & _
          "Customer_Addresses.Add3, Customer_Addresses.Add4, Customer_Addresses.Add5, " & _
          "Customer_Addresses.Postcode, Customer_Addresses.Country, Customer_Addresses.Telephone, " & _
          "Customer_Addresses.Fax, Customer_Addresses.Email, Customer_Addresses.Mobile_Phone " & _
          "FROM Customers AS c INNER JOIN Customer_Addresses AS a ON c.Customer_Code = a.Cust_Code"

   Dim da As New OleDbDataAdapter(sql, con)
   Dim ds As New DataSet
   Dim dt As New DataTable

   da.Fill(ds)
   dt = ds.Tables(0).Copy()

   dgvExport.DataSource = dt

导致以下错误;

  

没有给出一个或多个必需参数的值

2 个答案:

答案 0 :(得分:1)

首先,您应该使用INNER JOIN

其次,要使用内部联接,您需要确保要加入的键在Access中具有关系。在创建新查询并将两个表(In Access)添加到查询时,它将告诉您它们之间没有关系。按“确定”按钮将转到关系设计器屏幕。

Customers表中选择Primary KeyCustomer_Code),然后将其拖到其他2个表(Cust_Code)中的匹配键中。这将允许数据库将键加在一起,而不是要求参数值。

查询本身应该如下所示;

sql = "SELECT [Customer_Addresses].[Cust_Code], " & _
   "[Customer_Addresses].[Contact_Code], [Customer_Addresses].[Contact_Name], " & _
   "[Customer_Addresses].[Contact_Type], [Customer_Addresses].[Add1], " & _
   "[Customer_Addresses].[Add2], [Customer_Addresses].[Add3], " & _
   "[Customer_Addresses].[Add4], [Customer_Addresses].[Add5], " & _
   "[Customer_Addresses].[Postcode], [Customer_Addresses].[Country]," & _
   "[Customer_Addresses].[Telephone], [Customer_Addresses].[Fax], " & _
   "[Customer_Addresses].[Email], [Customer_Addresses].[Mobile_Phone], " & _
   "[Customers].[Customer_Code], [Customers].[Customer_Category], " & _
   "[Customers].[Average_Payment_Terms], [Customers].[Customer_Name], [Customers].[Notes], " & _
   "[Customers].[Aq_Date], [Customers].[salesRep], " & _
   "[Customers].[hoEmail], [Customers].[webpage] FROM Customers " & _
   "INNER JOIN Customer_Addresses ON [Customers].[Customer_Code] =[Customer_Addresses].[Cust_Code]"

显然这仅适用于两个表,但是一旦您加入Primary KeyDelivery_Addresses.Cust_Code,它也会包含此表。

另外需要注意的是,如果您有100位客户,我们会说Customer_Addresses中有1位地址,但只有5位客户的地址位于Delivery_Addresses,只会显示5位有送货地址的客户。它将显示来自所有3个表的数据,但仅显示所有表中存在该客户数据的记录。

也许只在一个表中存储地址是更好的方法。

答案 1 :(得分:-2)

改为使用JOIN:

sql = "SELECT C.Customer_Code, C.Customer_Name, C.Customer_Category, " & _
   "C.Average_Payment_Terms, C.Notes, C.Aq_Date, C.salesRep, C.hoEmail, C.webpage, A.Contact_Code, A.Contact_Type, A.Contact_Name, A.Add1, A.Add2, " & _
   "A.Add3, A.Add4, A.Add5, A.Postcode, A.Country, A.Telephone, A.Fax, " & _
   "A.Email, A.Mobile_Phone FROM Customers C INNER JOIN Customer_Addresses A ON C.Customer_Code=A.Cust_Code "