在内连接查询中需要帮助

时间:2012-11-21 10:58:00

标签: sql oracle inner-join

SELECT table1.*,table2.*, table3.*
FROM table1, table2, table3
inner join 
 (
   select table1.id AS pid, max(table1.createtime) AS timestamp
   from table1 group by table1.id
 ) A on A.pid = table1.id AND A.timestamp = table1.createtime
WHERE table3.id = table1.id
ORDER BY table1.createtime;

我正在尝试仅获取table1中每个id的最后修改记录。

对于上述查询,我​​收到的错误为:

"%s:invalid identifier" for  "A.timestamp = table1.IDA2A2" this part.

请告诉我这个查询有什么问题。  DB是Oracle。

1 个答案:

答案 0 :(得分:0)

您的问题的一部分是混合JOIN语法 - 您的查询使用逗号连接和ANSI JOIN语法。您需要正确加入表格:

SELECT t1.,t2., t3.* 
FROM table1 t1
INNER JOIN table2 t2
  on t1.id = t2.id
INNER JOIN table3 t3
  on t1.id = t3.id
inner join 
(
  select table1.id AS pid, max(table1.createtime) AS timestamp 
  from table1 
  group by table1.id
) A 
  on A.pid = t1.id 
  AND A.timestamp = t1.createtime 
ORDER BY t1.createtime;