简体   繁体   English

Access 2007中的SQL-语法错误

[英]SQL in Access 2007 - Syntax error

I am using Access 2007 to create an SQL query to join two tables. 我正在使用Access 2007创建一个SQL查询以联接两个表。 I was able to do that but then I don't have the rows where the columns from the second table are NULL; 我能够做到这一点,但是然后我没有第二个表中的列为NULL的行; I just have the rows where there is information in the second table that matches. 我只有第二个表中有匹配信息的行。 I tried to do a LEFT JOIN but Access didn't like this. 我试图做一个LEFT JOIN,但是Access不喜欢这样。 Instead I am trying to create a better 'join/on' addition to my query (seen below) but now I am getting a "Syntax error in FROM clause". 相反,我正在尝试为查询创建更好的“ join / on”(如下所示),但是现在我收到“ FROM子句中的语法错误”。 Do you have any ideas what I am doing wrong? 您有什么想法我做错了吗?

SELECT *
FROM dbo_table1 AS t1
JOIN dbo_table2 AS t2
ON (
     (t1.id = t2.id)   // where the two ids match so the info in table 2 match with table 1
     OR 
     ((SELECT COUNT(*) FROM dbo_table2 AS t3 WHERE t1.id = t3.id)=0)  // there is no match but I want the row with NULL for the values from the second table
   );

If you want all rows from dbo_table1, whether or not there is a matching row in dbo_table2, use a LEFT JOIN. 如果要dbo_table1中的所有行,无论dbo_table2中是否有匹配的行,请使用LEFT JOIN。 Access should accept this one: Access应该接受以下一项:

SELECT *
FROM
    dbo_table1 AS t1
    LEFT JOIN dbo_table2 AS t2
    ON t1.id = t2.id;

You can have an outer / cross join with a WHERE statement (a.id=b.id or b.id is null). 您可以使用WHERE语句进行外部/交叉联接(a.id = b.id或b.id为null)。

Or a UNION, the first being all a.id = b.id, the second where b.id is null. 或一个UNION,第一个都是a.id = b.id,第二个都是b.id为null。

(Depending on your exact requirements) (取决于您的确切要求)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM