手动联接表 (Visual Database Tools)Join Tables Manually (Visual Database Tools)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse

向查询中添加两个或多个表时,查询和视图设计器将尝试根据公共数据或数据库中存储的关于这些表如何相关的信息来联接这些表。When you add two (or more) tables to a query, the Query and View Designer attempts to join them based on common data or on information stored in the database about how tables are related. 有关详细信息,请参阅自动联接表 (Visual Database Tools)For details, see Join Tables Automatically (Visual Database Tools). 但是,如果查询和视图设计器未自动联接这些表,或者您希望在这些表之间创建其他联接条件,则可手动联接这些表。However, if the Query and View Designer has not joined the tables automatically, or if you want to create additional join conditions between tables, you can join tables manually.

除基于包含相同信息的列之外,还可以基于任意两列之间的比较创建联接。You can create joins based on comparisons between any two columns, not just columns that contain the same information. 例如,如果数据库包含 titlesroysched两个表,则可将 ytd_sales 表的 titles 列中的值与 lorange 表的 hirangeroysched 列中的值相比较。For example, if your database contains two tables, titles and roysched, you can compare values in the ytd_sales column of the titles table against the lorange and hirange columns in the roysched table. 创建此联接将允许您查找特定的书名,这些书籍截止到目前为止的年销售额介于版税的最高和最低范围之内。Creating this join would allow you to find titles for which the year-to-date sales falls between the low and high ranges for the royalty payments.

提示

如果联接条件中的列已建立索引,则联接的速度最快。Joins work fastest if the columns in the join condition have been indexed. 在某些情况下,对没有建立索引的列进行联接会导致查询速度缓慢。In some cases, joining on unindexed columns can result in a slow query.

手动联接表或表结构对象To manually join tables or table-structured objects

  1. 将要联接的对象添加到 “关系图”窗格 中。Add to the Diagram pane the objects you want to join.

  2. 拖动第一个表或表结构对象中的联接列的名称,并将其拖放到第二个表或表结构对象中的相关列上。Drag the name of the join column in the first table or table-structured object and drop it onto the related column in the second table or table-structured object. 不能基于 textntext、或image 列建立联接。You cannot base a join on text, ntext, or image columns.

    备注

    联接列必须具有相同(或兼容)的数据类型。The join columns must be of the same (or compatible) data types. 例如,如果第一个表中的联接列是日期,则必须将其与第二个表中的日期列相关。For example, if the join column in the first table is a date, you must relate it to a date column in the second table. 另一方面,如果第一个联接列是整数,则相关联接列也必须是整数数据类型,但它的大小可以不同。On the other hand, if the first join column is an integer, the related join column must also be of an integer data type, but it can be a different size. 查询和视图设计器不检查要用来创建联接的列的数据类型,但当您执行查询时,如果数据类型不兼容,数据库将显示错误。The Query and View Designer will not check the data types of the columns you use to create a join, but when you execute the query, the database will display an error if the data types are not compatible.

  3. 如果需要,请更改联接运算符;默认情况下,该运算符是等号 (=)。If necessary, change the join operator; by default, the operator is an equal sign (=). 有关详细信息,请参阅修改联接运算符 (Visual Database Tools)For details, see Modify Join Operators (Visual Database Tools).

查询和视图设计器将在 SQL 窗格内的 SQL 语句中添加 INNER JOIN 子句。The Query and View Designer adds an INNER JOIN clause to the SQL statement in the SQL pane. 您可以将其类型更改为外部联接。You can change the type to an outer join. 有关详细信息,请参阅创建外部联接 (Visual Database Tools)For details see Create Outer Joins (Visual Database Tools).

另请参阅See Also

使用联接进行查询 (Visual Database Tools)Query with Joins (Visual Database Tools)