我正处于报告创建过程中。我通过这些表格结构来看,这真的很复杂。我无法创建一个视图。
我有两个结构表
DBObjectType
-------------
TypeID,
Description
和
DBObAssign
----------
ID
ReferenceObType
ReferenceObID
TargetObType
TargetObID
该表格填充如下:
DBObjectType table:
+--------+-------------------+
| TypeID Description |
+--------+-------------------+
| 1 Account |
| 2 TFN |
| 3 VDN |
| 4 Skill |
+--------+-------------------+
DBObAssign table
+----+-------------+---------+---------+--------+
| ID RefObType RefObID TgtObType TgtObID |
+----+-------------+---------+---------+--------+
| 1 1 12 2 23 | => refer TFN Table
| 2 1 12 3 12 | => refer VDN Table
| 3 1 23 4 1 | => refer Skill Table
| 4 1 23 2 45 | => refer TFN Table
| 5 1 23 4 31 | =>refer Skill Table
+----+-------------+---------+---------+--------+
我的报告需要显示帐户的所有TFN,VDN和技能
布局就像:
+------+-------+------------+---------+
|Account TFN Skill VDN |
+------+-------+------------+---------+
| 12 TFNof12 VDN@@@ |
| 23 TFNof23 Skill1of23 |
| 23 Skill2 of 23 |
+------+-------+------------+---------+
现在我只能与一张桌子联系。这意味着我只是加入帐户表以获取TFN详细信息。
select AccountName,TFN,Skill from tblAccount as acc
left outer join tblDBAssign as DBA on DBA.RefObID = acc.id
left outer join tblDBobject as DBo on DBo.Description= 'TFN'
有没有办法从技能和vdn表中获取其他记录?请提供一些建议。
的表格结构
VDN:
---
VDNID
VDNDesc
TFN:
----
TFNID
TFNOwner
TFNDesc
Skill:
------
SkillID
SkillDesc
答案 0 :(得分:0)
你的脚本看起来应该是这样的(请注意,这不是测试,也不清楚表名,这与你在结构和sql示例中给出的不同):
SELECT AccountName, t.TFNDesc, v.VDNDesc, s.SkillDesc
FROM tblAccount AS acc
LEFT OUTER JOIN tblDBAssign as DBA1
ON DBA1.RefObID = acc.id
LEFT OUTER JOIN tblDBobject as DBo1
ON DBo1.Description= 'TFN'
AND DBA1.TgtObType = DBo1.TypeID
LEFT OUTER JOIN TFN as t
ON t.TFNID = DBA1.TgtObID
LEFT OUTER JOIN tblDBAssign as DBA2
ON DBA2.RefObID = acc.id
LEFT OUTER JOIN tblDBobject as DBo2
ON DBo2.Description= 'VDN'
AND DBA2.TgtObType = DBo2.TypeID
LEFT OUTER JOIN VDN as v
ON v.VDNID = DBA2.TgtObID
LEFT OUTER JOIN tblDBAssign as DBA3
ON DBA3.RefObID = acc.id
LEFT OUTER JOIN tblDBobject as DBo2
ON DBo3.Description= 'Skill'
AND DBA3.TgtObType = DBo3.TypeID
LEFT OUTER JOIN Skill as s
ON s.SkillID = DBA3.TgtObID