当需要从同一个表引用两列时,我需要在数据库中创建一个视图。我可以创建一个这样的视图:
CREATE VIEW [dbo].[ViewJournal]
AS
SELECT
J. d, J.date, J.drac, L.name as draccount, J.crac,
L.name as craccount, J.dramt, J.cramt, J.lf,
J.description, J.voucherType, J.reg_date, J.last_update,
J.active
FROM
Journal J, Ledger L
WHERE
J.drac = L.Id
但结果并没有显示实际结果。
此处,crac
和drac
来自Ledger
表格。
Journal
表:
CREATE TABLE [dbo].[Journal]
(
[Id] DECIMAL (18) IDENTITY (1, 1) NOT NULL,
[date] DATETIME NULL,
[drac] DECIMAL (18) NULL,
[crac] DECIMAL (18) NULL,
[dramt] DECIMAL (18, 2) NULL,
[cramt] DECIMAL (18, 2) NULL,
[reg_date] DATETIME NULL,
[last_update] DATETIME NULL,
[active] INT NULL,
[lf] VARCHAR (50) NULL,
[description] NVARCHAR (150) NULL,
[voucherType] VARCHAR (50) NULL,
[sales] VARCHAR (50) NULL,
[purchase] VARCHAR (50) NULL,
[cash_paymentno] VARCHAR (50) NULL,
[cash_receiptno] VARCHAR (50) NULL,
[expense] VARCHAR (50) NULL,
[income] VARCHAR (50) NULL,
[advance] VARCHAR (50) NULL,
[remunaration] VARCHAR (50) NULL,
CONSTRAINT [PK_Journal] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Ledger] FOREIGN KEY ([drac]) REFERENCES [dbo].[Ledger] ([Id]) ON DELETE CASCADE ON UPDATE CASCADE
);
Ledger
表:
CREATE TABLE [dbo].[Ledger]
(
[Id] DECIMAL (18) IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (50) NULL,
[type] NVARCHAR (50) NULL,
[classification] VARCHAR (50) NULL,
[realornominal] VARCHAR (50) NULL,
[reg_date] DATETIME NULL,
[last_update] DATETIME NULL,
[active] DECIMAL (2) NULL,
[depree] VARCHAR (50) NULL,
CONSTRAINT [PK_Ledger] PRIMARY KEY CLUSTERED ([Id] ASC)
);
答案 0 :(得分:2)
在您当前的查询中,您加入了J.drac = L.Id
,这意味着L
始终是J.drac
中引用的记录,无论{的值是多少{1}}。
我理解它的方式,你想在J.crac
表中引用两个不同的记录。你需要两个连接。
Ledger