0

我有四个表要加入并一起显示输出。我不确定 Oracle SQL Developer 的语法是如何工作的。我知道这对程序员来说是一个简单的问题,我希望有人可以就代码的外观提出建议。

表是表是:JNL1 JNL2 JNL3 JNL4

JNL1
JNL2
JNL3
JNL4

我只想使用 UserCode=Automation 显示结果,并且 Date 比当前日期早一天。代码是:

  UserCode = 'Automation' AND     
  CONVERT(VARCHAR,LEFT(DATE,8))=CONVERT(VARCHAR(8),GETDATE()-1,112)

所有这四个表之间共有的键是 ItemID、Date 和 UserCode。

我不确定查询会是什么样子,我可以将连接表查询和 UserCode/Date 查询生成为单独的查询。但是,当我将它们组合到一个查询中时,我要么在不明确的列名上出现错误,要么在运行查询时没有从数据库中显示任何数据。

4

1 回答 1

2

不要加入表格。因为您只需要从一个表中获取数据并且表结构相同,那么您可以拥有多个unions

-- 在 oracle 数据库中,您不能将列名作为日期,我假设date列名是 c_date-

select ItemID, c_Date, and UserCode
from Jul1
where UserCode = 'Automation' 
  AND c_date = trunc(sysdate) -1
union all 
select ItemID, c_Date, and UserCode
from Jul2
where UserCode = 'Automation' 
  AND c_date = trunc(sysdate) -1
union all 
select ItemID, c_Date, and UserCode
from Jul3
where UserCode = 'Automation' 
  AND c_date = trunc(sysdate) -1
union all 
select ItemID, c_Date, and UserCode
from Jul4
where UserCode = 'Automation' 
  AND c_date = trunc(sysdate) -1

*sysdate是系统函数,它将返回当前日期和时间值。

于 2013-06-23T23:25:51.810 回答