簡體   English   中英

找出兩個表的差異

[英]find two tables differences

我們有一個 Oracle sql 像:

with temp1 as (
 select col1,col2,col3,col4,col5 from a 
), temp2 as (
   select col1,col2,col3,col4,col5 from b 
)
select *
from
(
( select * from temp1
minus
select * from temp2  )
union
( select * from temp2 
minus
select * from temp1  )
)
union
(SELECT * FROM temp1 T1
LEFT JOIN temp2 T2 ON T1.col1 = T2.col1
WHERE T1.col2 <> T2.col2 )

但它拋出“ORA-01789:查詢塊的結果列數不正確”,

你能提供一些建議嗎?

目前還不清楚你想做什么。 但是,這部分返回 10 列,而不是 5:

SELECT *
FROM temp1 T1 LEFT JOIN
     temp2 T2
     ON T1.col1 = T2.col1

所以列數不一樣。

明確列出所有子查詢的列 - 使用別名。

你得到的錯誤是因為這個:

with 
temp1 as (
  select col1,col2,col3,col4,col5 from a    --> TEMP1 has 5 columns
), 
temp2 as (
  select col1,col2,col3,col4,col5 from b    --> TEMP2 has 5 columns
)
select *
from
(
( select * from temp1                       --> TEMP1 MINUS TEMP2 has 5 columns  --> OK
minus
select * from temp2  )
union
( select * from temp2                       --> TEMP2 MINUS TEMP1 has 5 columns  --> OK
minus
select * from temp1  )
)
union
(SELECT * FROM temp1 T1                     --> TEMP1 JOIN TEMP2 has 10 columns  --> WRONG
LEFT JOIN temp2 T2 ON T1.col1 = T2.col1
WHERE T1.col2 <> T2.col2 )

如果最后一個查詢(“錯誤”查詢)被重寫為例如select t1.* from... ,它會起作用,但問題是這是否是你想要的。

最后一個查詢在 select 列表中有 10 列,其他查詢有 5 個。最后一個查詢應該是 t1.* 或 t2.* 或任何自定義但有 5 列

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM