多个表之间的查询仅选择BOTH表中的条目

时间:2015-05-07 16:56:08

标签: ms-access

在多个表之间运行查询时,如何返回任一表中所有条目的结果,而不是仅返回BOTH表中的条目?

1 个答案:

答案 0 :(得分:-1)

您需要使用Access不支持的FULL OUTER JOIN。但是有一种解决方法:

Show all rows from both tables, and join them where a common value exists

When you want to show all rows from two tables and join them based on common values, you use a full outer join. Access does not explicitly support full outer joins, but you can achieve the same effect by using a union query. The following procedure explains how to do this, but if you want more information about union queries, see the See Also section.

To use a union query to perform a full outer join:

Create a query that has a left outer join on the field that you want use for a full outer join.

On the Home tab, in the Views group, click View, and then click SQL View.

Press CTRL+C to copy the SQL code.

Delete the semicolon at the end of the FROM clause, and then press ENTER.

Type UNION, and then press ENTER.

NOTE   Do not use the ALL keyword when you use a union query to perform a full outer join.

Press CTRL+V to paste the SQL code that you copied in step 3.

In the code that you pasted, change LEFT JOIN to RIGHT JOIN.

Delete the semicolon at the end of the second FROM clause, and then press ENTER.

Add a WHERE clause that specifies that the value of the join field is NULL in the first table listed in the FROM clause (the left table).

For example, if the FROM clause is:

 FROM Products RIGHT JOIN [Order Details] 
 ON Products.ID = [Order Details].[Product ID]
You would add the following WHERE clause:

WHERE Products.ID IS NULL

Type a semicolon (;) at the end of the WHERE clause to indicate the end of the union query.

On the Design tab, in the Results group, click Run.

这假设您要将条目与共享值合并。

相关问题