简体   繁体   English

Oracle SQL连接三个表

[英]Oracle SQL join three tables

I have three Tables and I wanted the join query or relevant for the below mentioned results 我有三个表,我想要加入查询或与以下提到的结果相关

 Types ( Table One)
 -------
 typeId
 -------
 square
 circle
 triangle


 Sets ( Table Two)
 ------
 id setValue setName
 ------------------
 1    one      setOne
 2    two      setTwo
 3    three    setThree


 Formation ( Table Three ) Types and sets are foreign keys to the other tables
 ---------
 id setValue  types     extra   notes
 --------------------------------------
 1     two     circle     5       xyz
 2     three   square     4       abc

I want to join this table and get result in such way , always i should get all the types displayed for the input gives as 我想加入该表并以这种方式获取结果,总是我应该为输入给出的所有类型显示为

if User choose set value as 'two' then result should be 如果用户选择设置值为“二”,则结果应为

 types      setValue        extra       notes
 --------------------------------------------
 square     null            null        null
 circle     two             5           xyz
 triangle   null            null        null

if user choose type value as 'square' then the result should be 如果用户选择类型值作为“平方”,则结果应为

  types     setValue        extra       notes
 --------------------------------------------
 square     three           4           abc
 circle     null            null        null
 triangle   null            null        null

if suppose user choose set value as 'rectangle' then the result should be 如果假设用户选择设置值为“矩形”,则结果应为

 types      setValue        extra       notes
--------------------------------------------
 square     null            null        null
 circle     null            null        null
 triangle   null            null        null

Looking forward for solution oracle 10g compatible code 期待解决方案与Oracle 10g兼容的代码

SELECT F.types, F.setValue, F.extra, F.notes
FROM types t
LEFT JOIN Formation F
 on t.typeID = F.Types
 AND ((F.SetValue = 'YourSetInput')
  OR (F.Types = 'YourTypesInput'))

By using a left outer join we always keep all the types. 通过使用左外部联接,我们始终保留所有类型。 By putting limiting criteria on the join and not in a where clause we don't have to worry about accidently excluding the null values you want to see. 通过在联接上而不是在where子句中设置限制条件,我们不必担心意外地排除了要查看的空值。

This however does have one draw back in that if both setvalue and types are keyed in both would return. 但是,这样做确实有一个缺点,即如果同时键入setvalue和type,则两者都会返回。 This may or may not be the desired result as the use case hasn't been covered. 这可能是预期结果,也可能不是预期结果,因为用例尚未涵盖。

So in recap 因此回顾

Return all the tables every time regardless of any other criteria. 不管其他条件如何,每次都返回所有表。
Then return the criteria from formation that matches the selected inputs for type or set. 然后从构成中返回与所选类型或集合输入匹配的条件。

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

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