简体   繁体   English

将多个表关联到一个表[SQL-Oracle 12c]

[英]Relating Multiple Tables to One Table [SQL - Oracle 12c]

I'll try my best to pose this as a question, and not a "write my script" request- but finding it hard to phrase without using the working example. 我将尽力将其作为一个问题提出,而不是提出“编写我的脚本”的请求-但在不使用工作示例的情况下很难措辞。

To start, here's a relational diagram of what I'm working with; 首先,这是我正在使用的关系图;

在此处输入图片说明

**TABLE 1 - TABLE 2 are scalable, ie there is a TABLE 4 - TABLE 5, TABLE -... etc, it's Tables, A/B/C that are constant. **表1-表2是可伸缩的,即有一个表4-表5,表-...等,它是恒定的表A / B / C。

Essentially, what I'm trying to do with this structure is match a certain value that only exists in TABLE 1 or 2 to TABLE C, where I'll pass that value to TABLE 1 or 2 and return the TABLE C value. 本质上,我要使用此结构进行的操作是将某个仅存在于表1或2中的值与表C匹配,在此我将该值传递给表1或2并返回表C的值。

SELECT
TABLEC.CUSTOMID
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.TABLE2ENTITYKEY = TABLE2.ENTITYKEY 
INNER JOIN TABLEA ON TABLEA.ENTITYKEY = TABLE2.TABLEAENTITYKEY 
INNER JOIN TABLEB ON TABLEB.ENTITYKEY = TABLEA.TABLEBENTITYKEY 
INNER JOIN TABLEC ON TABLEC.ENTITYKEY = TABLEB.TABLECENTITYKEY 
WHERE TABLE1.USERENTITYKEY = /*ENTER_VALUE*/

OR 要么

SELECT
TABLEC.CUSTOMID
FROM TABLE3
INNER JOIN TABLE4 ON TABLE3.TABLE4ENTITYKEY = TABLE4.ENTITYKEY 
INNER JOIN TABLEA ON TABLEA.ENTITYKEY = TABLE4.TABLEAENTITYKEY 
INNER JOIN TABLEB ON TABLEB.ENTITYKEY = TABLEA.TABLEBENTITYKEY 
INNER JOIN TABLEC ON TABLEC.ENTITYKEY = TABLEB.TABLECENTITYKEY 
WHERE TABLE3.USERENTITYKEY = /*ENTER_VALUE*/

Both of these work, and will work for my Tables 5/6, 7/8 etc - but that leaves me with multiple queries. 这些都可以使用,并且可以用于我的表5/6,表7/8等-但这给我留下了很多查询。 What I'd like to do is 'combine' these queries into one, where I'll pass my value to Tables 1/3/5 etc and return any values from C that link back to any of the source tables - it's not important that I know which source table the value in C links too, it's only important that it links to one of them. 我想做的是将这些查询“组合”为一个,在其中将我的值传递到表1/3/5等,并从C返回返回链接到任何源表的任何值-这并不重要我也知道C中的值链接到了哪个源表,将它链接到其中一个也很重要。

Does anyone have any suggestions or examples I can use to see how this kind of script is formatted? 有没有人有任何建议或示例可供我用来查看这种脚本的格式? I have played around with some things - but my SQL isn't that strong, so it's futile so far. 我玩过一些东西-但是我的SQL不够强,所以到目前为止还没有用。

Thanks in advance, please comment if more information is required - and sorry in advance if the question is poorly asked! 在此先感谢您,如果需要更多信息,请发表评论-如果问题提出不当,请先抱歉!

-L -L

这不是一个完整的解决方案-但是目前使用UNION将上述两个查询绑定在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM