我意识到我正在使用的数据库中的数据结构还有很多不足之处,但我无法控制,所以请耐心等待。
我所拥有的是一个ADDRESSES.COMPANY
可能是也可能不是整数 ID 的字段。有时它是一个字符串。但是,当它是整数时,我想加入另一个表COMPANIES
。原始查询(预加入)如下所示:
SELECT *
FROM (SELECT ROWNUM AS ROW_NUM, INNERQUERY.*
FROM ( SELECT *
FROM ADDRESSES, COMPANIES
) INNERQUERY) OUTERQUERY
WHERE ROW_NUM <= 50 AND ROW_NUM > 0
如果我尝试正常加入,我会收到一个错误ORA-01722: invalid number
......原因很明显(尝试加入的并不总是一个数字,或者至少我认为这就是它必须的意思,因为它是真的!)。尝试加入后我的查询:
SELECT *
FROM (SELECT ROWNUM AS ROW_NUM, INNERQUERY.*
FROM ( SELECT *
FROM ADDRESSES, COMPANIES
WHERE ADDRESSES.COMPANY(+) = COMPANIES.ID
) INNERQUERY) OUTERQUERY
WHERE ROW_NUM <= 50 AND ROW_NUM > 0
我能想出的唯一两个潜在解决方案都是使用该TO_NUMBER()
函数,我认为它会将非数字字符串评估为 0。我试过:
SELECT *
FROM (SELECT ROWNUM AS ROW_NUM, INNERQUERY.*
FROM ( SELECT *
FROM ADDRESSES, COMPANIES
WHERE TO_NUMBER(ADDRESSES.COMPANY)(+) = COMPANIES.ID
) INNERQUERY) OUTERQUERY
WHERE ROW_NUM <= 50 AND ROW_NUM > 0
哪个错误ORA-00936: missing expression
(在第 7 行)。我假设我对使用它的适当位置的猜测不正确,并尝试了以下方法:
SELECT *
FROM (SELECT ROWNUM AS ROW_NUM, INNERQUERY.*
FROM ( SELECT *, TO_NUMBER(ADDRESSES.COMPANY) AS COMPANYID
FROM ADDRESSES, COMPANIES
WHERE COMPANYID(+) = COMPANIES.ID
) INNERQUERY) OUTERQUERY
WHERE ROW_NUM <= 50 AND ROW_NUM > 0
第 3 行出错ORA-00923: FROM keyword not found where expected
。
当我的第一次尝试没有成功时,我确信我的第二次会成功。但我对这个错误感到困惑。我对 Oracle 语法和/或行为完全一无所知,还是别的什么?我的问题有解决方案吗?