Is it possible to use subquery in join condition in Access?

瘦欲@ 提交于 2019-12-23 07:22:53

问题


In postgresql I can use subquery in join condition

SELECT * 
FROM table1 LEFT JOIN table2
     ON table1.id1 = (SELECT id2 FROM table2 LIMIT 1);

But when I try to use it in Access

SELECT *
FROM table1 LEFT JOIN table2 
     ON table1.id1 = (SELECT TOP 1 id2 FROM table2);

I get syntax error. Is it actually impossible in Access or just my mistake?

I know that I can get the same result with WHERE, but my question is about possibilities of JOIN in Access.


回答1:


It's not possible, per the MSDN documentation:

Syntax

FROM table1 [ LEFT | RIGHT ] JOIN table2 ON table1.field1 compopr table2.field2

And (emphasis mine):

field1, field2: The names of the fields that are joined. The fields must be of the same data type and contain the same kind of data, but they do not need to have the same name.

It appears you can't even have hard-coded values in your join; you must specify the column name to join against.

In your case, you would want:

SELECT *
FROM Table1
LEFT JOIN (
    SELECT DISTINCT TOP 1 ID 
    FROM Table2
    ORDER BY ID
) Table2Derived ON Table1.ID = Table2Derived.ID


来源:https://stackoverflow.com/questions/13567772/is-it-possible-to-use-subquery-in-join-condition-in-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!