【发布时间】:2014-02-28 15:06:33
【问题描述】:
使用 Oracle,我有需要查询的表。有一个主表,其中包含一个包含所有其他表的名称的字段。 (每行一个)
第一个表 (table1) 有一列包含第二个表 (table1.table2name) 的表名。
第二个表有一个日期字段(datetocheck)。如果第二个表中没有超过 6 个月的记录,我只想从第一个表中带回记录(table2 有日期,table1 有我要显示的数据)。
应该是这样的格式:
select * from table1
where not exists (
select * from table1.table2name
where datetocheck > add_months(sysdate, -6)
)
提前致谢
其他信息
有一个 table1 和 MANY table2。
例如
主表(table1)
记录 1:column:table2name='Table2a'
记录 2:column:table2name='Table2b'
记录 3:column:table2name='Table2c'
附加表(table2x)
---表2a
记录 1:column:datetocheck='01/01/1980'
记录 2:column:datetocheck='01/01/1990'
记录 3:column:datetocheck='01/01/2000'
---表2b
记录 1:column:datetocheck='01/01/1982'
记录 2:column:datetocheck='01/01/1983'..etc
---表2c
记录 1:column:datetocheck='01/01/2014'
记录 2:column:datetocheck='02/01/2014'..etc
注意 #1:table1 和 table2x 之间的唯一关系是 table1 中的 table2name 列和 table2x 中的实际表名。
【问题讨论】:
-
好的,那你有什么尝试?
-
不能在 SQL 语句中动态引用表名。您需要准备好的语句或游标之类的东西。