加入SQL表

时间:2014-12-23 22:08:50

标签: sql sql-server

我有一个客户表,其中包含客户的主要信息,如姓名,姓氏,密码......我有一个地址,电子邮件,电话表,例如3种地址,2个电话号码,2个电子邮件地址每个客户。我有一个Type表,TypeID和Type_Group,Type_Value。例如:

  TypeID           Type_Group       Type_Value
    1               Address          Work
    2               Address          Home
    3               Address          mailing
    4               Email            Primary
    5               Email            secondary

我知道如何加入客户表与地址,电子邮件和电话表。我不知道如何加入地址,电话,电子邮件类型表。

这是我的查询:

     SELECT
      cc.[Customer_ID]
        ,[Account_Number]
        ,[First_Name]
        ,[Middle_Name]
        ,[Last_Name]
        ,[Password]
     ,ce.[Email]
     ,cph.Phone_Number
     ,ca.Address_1
     ,ca.Address_2
     ,ca.City
     ,ca.State
     ,ca.Zip
     ,tp.Type_Desc

   FROM [CustomerPortal].[dbo].[Customer] cc WITH (NOLOCK) 
   left join [CustomerPortal].[dbo].Customer_Email ce WITH (NOLOCK) on cc.Customer_ID =    ce.Customer_ID
   left join [CustomerPortal].[dbo].Customer_Address ca WITH (NOLOCK) on cc.Customer_ID =cp.Customer_ID
   left join [CustomerPortal].[dbo].Customer_Phone cph WITH (NOLOCK) on cc.Customer_ID =cph.Customer_ID

   WHERE cc.Customer_ID=@Customer_ID

这是表格:

这是客户表:

         (PRIMARY KEY)[Customer_ID] [int] IDENTITY(1,1) NOT NULL,
                      [Account_Number] [int] NULL,
                      [First_Name] [varchar](50) NULL,
                      [Middle_Name] [varchar](50) NULL,

客户地址表:

                      [dbo].[Customer_Address](
                      primary key[Customer_Address_ID] [int] IDENTITY(1,1) NOT NULL,
                      Fkey  [Customer_ID] [int] NOT NULL,
                      [Address_1] [varchar](100) NULL,
                      [Address_2] [varchar](100) NULL,
                      [City] [varchar](100) NULL,
                      [State] [varchar](10) NULL,
                      [Zip] [varchar](10) NULL,
                      Fkey  [Address_Type] [int] NULL,

CustomerEmail表:

               [dbo].[Customer_Email](
          PKey [Customer_Email_ID] [int] IDENTITY(1,1) NOT NULL,
          Fkey [Customer_ID] [int] NOT NULL,
               [Email] [varchar](50) NULL,
        Fkey   [Email_Type] [int] NULL,

客户电话表:

    PK       [dbo].[Customer_Phone](
    FK       [Customer_Phone_ID] [int] IDENTITY(1,1) NOT NULL,
             [Customer_ID] [int] NOT NULL,
             [Phone_Number] [bigint] NULL,
   FK        [Phone_Type] [int] NULL,

类型表:

      PK  [dbo].[Type_XREF](
       [Type_ID] [int] IDENTITY(1,1) NOT NULL,
       [Type_Group] [varchar](25) NULL,
       [Type_Value] [varchar](50) NULL,
       [Type_Desc] [varchar](100) NULL,

我不确定如何将连接添加到Type Table,我尝试的任何东西都会产生几行,因为我希望有一行代表客户,所有相关的家庭住址,所有相关的邮寄地址,主要电子邮件,二级电子邮件,....所以所有客户信息都在1行记录中。

1 个答案:

答案 0 :(得分:0)

您可以将每个表单独连接到类型表,因为它们的类型ID将对应于类型表中的一个ID。即使表中混合了类型,它们各自表中的每个元素也会合理地分配给它的逻辑类型。我只是假设你想要显示输入的格式和表的名称:

SELECT
  cc.[Customer_ID]
    ,[Account_Number]
    ,[First_Name]
    ,[Middle_Name]
    ,[Last_Name]
    ,[Password]
 ,ce.[Email]
 ,cph.Phone_Number
 ,ca.Address_1
 ,ca.Address_2
 ,ca.City
 ,ca.State
 ,ca.Zip
 ,tp.Type_Desc
 ,te.Type_Group + '-' + te.Type_Value as [EmailType]
 ,ta.Type_Group + '-' + ta.Type_Value as [AddressType]
 ,tph.Type_Group + '-' + tph.Type_Value as [PhoneType]

 FROM [CustomerPortal].[dbo].[Customer] cc WITH (NOLOCK) 

left join [CustomerPortal].[dbo].Customer_Email ce WITH (NOLOCK) on cc.Customer_ID =    ce.Customer_ID
left join [CustomerPortal].[dbo].Customer_Address ca WITH (NOLOCK) on cc.Customer_ID =cp.Customer_ID
left join [CustomerPortal].[dbo].Customer_Phone cph WITH (NOLOCK) on cc.Customer_ID =cph.Customer_ID
left join [dbo].Type_Table te on te.ID = ce.Type
left join [dbo].Type_Table ta on ta.ID = ca.Type
left join [dbo].Type_Table tph on tph.ID = cph.Type