2017-05-05 45 views
1

我有tbl_jobs.FromOrgID相應tbl_orgs.OrgName 2個表顯示相應的名字

tbl_jobs

CREATE TABLE [dbo].[tbl_jobs] 
(
    [JobID] [int] IDENTITY(1,1) NOT NULL, 
    [JobType] [nvarchar](50) NOT NULL, 
    [RequestID] [int] NOT NULL, 
    [AssignTo] [int] NOT NULL, 
    [FromOrgID] [int] NOT NULL, 
    [ToOrgID] [int] NOT NULL, 
    [Ammount] [nvarchar](50) NOT NULL, 
    [JobStatus] [nvarchar](50) NOT NULL, 
    [Remark] [nvarchar](50) NULL, 
    [strOwner] [nvarchar](50) NOT NULL, 
    [dbTstamp] [datetime2](7) NOT NULL, 

    CONSTRAINT [PK_tbl_jobs] 
     PRIMARY KEY CLUSTERED ([JobID] ASC) 
) ON [PRIMARY] 
GO 

ALTER TABLE [dbo].[tbl_jobs] WITH CHECK 
    ADD CONSTRAINT [FK_tbl_jobs_tbl_orgs] 
    FOREIGN KEY([FromOrgID]) REFERENCES [dbo].[tbl_orgs] ([OrgID]) 
GO 

ALTER TABLE [dbo].[tbl_jobs] CHECK CONSTRAINT [FK_tbl_jobs_tbl_orgs] 
GO 

ALTER TABLE [dbo].[tbl_jobs] WITH CHECK 
    ADD CONSTRAINT [FK_tbl_jobs_tbl_orgs1] 
    FOREIGN KEY([ToOrgID]) REFERENCES [dbo].[tbl_orgs] ([OrgID]) 
GO 

tbl_orgs

CREATE TABLE [dbo].[tbl_orgs] 
(
    [OrgID] [int] IDENTITY(1,1) NOT NULL, 
    [OrgName] [nvarchar](50) NOT NULL, 
    [OrgTele] [nvarchar](50) NULL, 
    [OrgEmail] [nvarchar](50) NULL, 
    [OrgArea] [nvarchar](50) NOT NULL, 
    [OrgCity] [nvarchar](50) NOT NULL, 
    [OrgLocation] [nvarchar](50) NOT NULL, 
    [OrgType] [nvarchar](50) NOT NULL, 
    [OrgStatus] [nvarchar](50) NOT NULL, 
    [strOwner] [nvarchar](50) NOT NULL, 
    [dbTStamp] [datetime2](7) NOT NULL, 

    CONSTRAINT [PK_tbl_orgs] 
     PRIMARY KEY CLUSTERED ([OrgID] ASC) 
) ON [PRIMARY] 
GO 

我需要得到大多數tbl_jobs列和& tbl_jobs.ToOrgID

如果我選擇tbl_orgs.orgname,我沒有得到正確的結果。

我被困在這裏。我應該用什麼類型的查詢來獲得結果?

enter image description here

回答

1

你即將加入兩次到同一個表的實例。你應該點擊「添加表」並再次添加tbl_orgs,並將「FromOrgID」加入其中一個實例,並將「ToOrgID」加入另一個實例。否則,連接沒有意義,除非「FromOrgID」和「ToOrgID」相等。

+1

非常感謝。這有幫助。 :) –