sql - 是否可以在 Access 的连接条件中使用子查询?

标签 sql ms-access join ms-access-2007 left-join

在 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/

相关文章:

mysql - 如何在规范化数据库中加入 tag_map

mysql - 如何存储数值的 unicode 字符,这就像简单的数据类型 int 一样工作

sql - 为什么 SQL 数据库在命令日志上使用预写日志?

python - 如何在 AsyncPG 中返回多行?

MySQL 添加、DATE_FORMAT 和 CONCAT_WS

sql - MS Access 中的左连接

mysql - 来自 MYSQL 数据库的 MS Access 报告

mysql - 了解连接

mysql - 从具有父子关系的单表创建 mySQL 连接表

SQL查询只获取在特定列中包含给定值的记录