我对一个简单的 SQL 查询有一个奇怪的行为。我在 Oracle 9i 上工作。我只是想确保我的查询中没有愚蠢的错误。如果有人能告诉我我做错了什么。这是查询:
select p.PARTNUM,
p.N_DATAA, dataa1.ID_DATA as ID_DATAA,
p.N_DATAF, dataf1.ID_DATA as ID_DATAF,
p.N_DATAS, datas1.ID_DATA as ID_DATAS
from PIECES p
left join DATA dataa1 on
dataa1.N_DATA = p.N_DATAA AND
dataa1.REV_DATA = ( select max(dataa2.REV_DATA) from DATA dataa2
where dataa2.N_DATA = p.N_DATAA )
left join DATA dataf1 on
dataf1.N_DATA = p.N_DATAF AND
dataf1.REV_DATA = ( select max(dataf2.REV_DATA) from DATA dataf2
where dataf2.N_DATA = p.N_DATAF )
left join DATA datas1 on
datas1.N_DATA = p.N_DATAS AND
datas1.REV_DATA = ( select max(datas2.REV_DATA) from DATA datas2
where datas2.N_DATA = p.N_DATAS )
where p.PARTNUM='MYPARTNUM';
结果如下:
"PARTNUM" "N_DATAA" "ID_DATAA" "N_DATAF" "ID_DATAF" "N_DATAS" "ID_DATAS"
"MYPARTNUM" "A23240" "300" "F4130" "398" "S2330" ""
我的问题出现在 ID_DATAS 字段上(我敢肯定,应该返回一个 ID_DATAS,见下文),它的行为就像它忽略了最后一个连接并且不检索任何数据。如果我删除第二个连接,它会完美运行。好像Oracle不支持超过2个左连接......(???)
这是我切换最后两个左连接时的另一个示例(在这种情况下,问题出现在 ID_DATAF 上):
select p.PARTNUM,
p.N_DATAA, dataa1.ID_DATA as ID_DATAA,
p.N_DATAF, dataf1.ID_DATA as ID_DATAF,
p.N_DATAS, datas1.ID_DATA as ID_DATAS
from PIECES p
left join DATA dataa1 on
dataa1.N_DATA = p.N_DATAA AND
dataa1.REV_DATA = ( select max(dataa2.REV_DATA) from DATA dataa2
where dataa2.N_DATA = p.N_DATAA )
left join DATA datas1 on
datas1.N_DATA = p.N_DATAS AND
datas1.REV_DATA = ( select max(datas2.REV_DATA) from DATA datas2
where datas2.N_DATA = p.N_DATAS )
left join DATA dataf1 on
dataf1.N_DATA = p.N_DATAF AND
dataf1.REV_DATA = ( select max(dataf2.REV_DATA) from DATA dataf2
where dataf2.N_DATA = p.N_DATAF )
where p.PARTNUM='MYPARTNUM';
结果如下:
"PARTNUM" "N_DATAA" "ID_DATAA" "N_DATAF" "ID_DATAF" "N_DATAS" "ID_DATAS"
"MYPARTNUM" "A23240" "300" "F4130" "" "S2330" "400"
谢谢您的帮助 :)
编辑:在查询中添加完全限定的列名(不能解决我的问题)