我有三个表和两个单独的SQL查询,它们正常工作,我得到了正确的结果。 如果我尝试加入这三个表,我的结果是null。
首先查询:
select T1.ID,T3.COMPANY
from T1,T3
where (T1.status!='CLOSED') and (T1.PRIORITY)>5 and T1.CLASSID=T3.CLASSID
第二次查询:
SELECT T1.ID, T2.DESCRIPTION
FROM T1
LEFT OUTER JOIN T2
ON T1.ID=T2.KEY
WHERE T1.status!='CLOSED'
AND (T2.CREATEDATE= (SELECT MAX(CREATEDATE)
FROM T2
WHERE T2.KEY=T1.ID))
我试图加入他们,但结果我有 null :
select T1.ID,T3.COMPANY,T2.DESCRIPTION
from T1
INNER JOIN T3 ON T1.CLASSID=T3.CLASSID
LEFT OUTER JOIN T2
ON T1.ID=T2.KEY
where (T1.status!='CLOSED') AND (T1.PRIORITY)>5
AND (T2.CREATEDATE= (SELECT MAX(CREATEDATE)
FROM T2
WHERE T2.KEY=T1.ID))
就像它没有识别从T2表中获取MAX值的最后一部分。
我做错了什么?谢谢你的帮助
答案 0 :(得分:2)
首先,在表T2上使用子查询的别名。
T2.CREATEDATE =
(SELECT MAX(T2Alias.CREATEDATE)
FROM T2 AS T2Alias
WHERE T2Alias.KEY = T1.ID)
其次,考虑将此条件移动到LEFT JOIN的ON子句到表T2。
答案 1 :(得分:0)
对我跳出的第一件事就是对T1.Priority>的新依赖性。 5和T2.CreateDate值等于内联查询的结果:
( AND (T1.PRIORITY) > 5
AND (T2.CREATEDATE =
(SELECT MAX(CREATEDATE) FROM T2 WHERE T2.KEY = T1.ID) )
如果没有数据,很难检查,但这可能是问题