简体   繁体   English

加入复杂的SQL Oracle选择查询

[英]Joining complex sql oracle select queries

I have a complex query that selects product id values from history and orders. 我有一个复杂的查询,该查询从历史记录和订单中选择产品ID值。

SELECT ProductID
FROM History h ( SELECT .....) LATEST
WHERE h.ProductId = LATEST.ProductID
AND ....
AND IsActive = true; 

This query is too long, so I could not write all of it. 该查询太长,因此我无法全部编写。 But it returns a table like this: 但是它返回一个这样的表:

ProdutID
--------
4654654
9879879
5465465
2132188
7894215
....

I want to join this product id result another table that containts ProductId column. 我想将此产品ID结果加入另一个包含ProductId列的表。

SELECT * FROM MySecondTable;

ProductID   Color
---------   -----
4654654     red
9879879     blue
5465465     orange

How can I join these two query? 如何加入这两个查询?

CTEs are a really simple way to do such a thing: CTE是执行此操作的一种非常简单的方法:

with t as (<your query here>)
select t.*, t2.color
from t join
     MySecondTable t2
     on t.ProductId = t2.ProductId;

You could also just add the join to the from clause of the original query. 您也可以只将join添加到原始查询的from子句中。

you could use the firts as inner joined table 您可以将firts用作内部联接表

  SELECT b.* 
  FROM MySecondTable b
  INNER JOIN (
      SELECT ProductID
      FROM History h ( SELECT .....) LATEST
      WHERE h.ProductId = LATEST.ProductID
      AND ....
      AND IsActive = true;
  ) t on t.ProductID = b.ProductID

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

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