在 postgresql 中,我可以在连接条件中使用子查询
SELECT *
FROM table1 LEFT JOIN table2
ON table1.id1 = (SELECT id2 FROM table2 LIMIT 1);
但是当我尝试在 Access 中使用它时
SELECT *
FROM table1 LEFT JOIN table2
ON table1.id1 = (SELECT TOP 1 id2 FROM table2);
我收到语法错误。在 Access 中实际上是不可能的还是只是我的错误?
我知道我可以用
WHERE
得到同样的结果,但我的问题是关于 JOIN
的可能性在 Access 中。
最佳答案
这是不可能的,根据 MSDN documentation :
Syntax
FROM table1 [ LEFT | RIGHT ] JOIN table2 ON table1.field1 compopr table2.field2
和(强调我的):
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.
看起来您甚至不能在您的连接中使用硬编码值;您必须指定要加入的列名。
在你的情况下,你会想要:
SELECT *
FROM Table1
LEFT JOIN (
SELECT DISTINCT TOP 1 ID
FROM Table2
ORDER BY ID
) Table2Derived ON Table1.ID = Table2Derived.ID
关于sql - 是否可以在 Access 的连接条件中使用子查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13567772/