Using Inner Joins An inner join is a join in which the values in the columns being joined are compared using a comparison operator.In the SQL-92 standard, inner joins can be specified in either the FROM or WHERE clause. This is the only type of join that SQL-92 supports in the WHERE clause. Inner joins specified in the WHERE clause are known as old-style inner joins.This Transact-SQL query is an example of an inner join:USE pubs SELECT * FROM authors AS a INNER JOIN publishers AS p ON a.city = p.city ORDER BY a.au_lname DESCThis inner join is known as an equi-join. It returns all the columns in both tables, and returns only the rows for which there is an equal value in the join column.Here is the result set:au_id au_lname au_fname phone address city ----------- -------- -------- ------------ --------------- -------- 238-95-7766 Carson Cheryl 415 548-7723 589 Darwin Ln. Berkeley 409-56-7008 Bennet Abraham 415 658-9932 6223 Bateman St. Berkeleystate zip contract pub_id pub_name city state country ----- ----- -------- ------ --------------------- -------- ----- ------- CA 94705 1 1389 Algodata Infosystems Berkeley CA USA CA 94705 1 1389 Algodata Infosystems Berkeley CA USA (2 row(s) affected)
具体请看一下SQL SERVER 的帮助或是看一下SQL的书籍,这里我贴一段帮助中的内容(是上面那位兄弟贴出来的中文版帮助,呵呵) 内联接是用比较运算符比较要联接列的值的联接。 --------------------------- 使用内联接 在 SQL-92 标准中,内联接可在 FROM 或 WHERE 子句中指定。这是 WHERE 子句中唯一一种 SQL-92 支持的联接类型。WHERE 子句中指定的内联接称为旧式内联接。下面的 Transact-SQL 查询是内联接的一个示例:USE pubs SELECT * FROM authors AS a INNER JOIN publishers AS p ON a.city = p.city ORDER BY a.au_lname DESC此内联接称为相等联接。它返回两个表中的所有列,但只返回在联接列中具有相等值的行。下面是结果集:au_id au_lname au_fname phone address city ----------- -------- -------- ------------ --------------- -------- 238-95-7766 Carson Cheryl 415 548-7723 589 Darwin Ln. Berkeley 409-56-7008 Bennet Abraham 415 658-9932 6223 Bateman St. Berkeleystate zip contract pub_id pub_name city state country ----- ----- -------- ------ --------------------- -------- ----- ------- CA 94705 1 1389 Algodata Infosystems Berkeley CA USA CA 94705 1 1389 Algodata Infosystems Berkeley CA USA (2 row(s) affected)在结果集中,city 列出现两次。由于重复相同的信息没有意义,因此可以通过更改选择列表消除两个相同列中的一个。其结果称为自然联接。可以重新表述前面的 Transact-SQL 查询以形成自然联接。例如:USE pubs SELECT p.pub_id, p.pub_name, p.state, a.* FROM publishers p INNER JOIN authors a ON p.city = a.city ORDER BY a.au_lname ASC, a.au_fname ASC下面是结果集:pub_id pub_name state au_id au_lname au_fname ------ --------------- -------- ----------- -------- -------- 1389 Algodata Infosystems CA 409-56-7008 Bennet Abraham 1389 Algodata Infosystems CA 238-95-7766 Carson Cherylphone address city state zip contract --------------- ------------- -------- ----- ----- --------- 415 658-9932 6223 Bateman St. Berkeley CA 94705 1 415 548-7723 589 Darwin Ln. Berkeley CA 94705 1(2 row(s) affected)本示例中,publishers.city 没有出现在结果中。
An inner join is a join in which the values in the columns being joined are compared using a comparison operator.In the SQL-92 standard, inner joins can be specified in either the FROM or WHERE clause. This is the only type of join that SQL-92 supports in the WHERE clause. Inner joins specified in the WHERE clause are known as old-style inner joins.This Transact-SQL query is an example of an inner join:USE pubs
SELECT *
FROM authors AS a INNER JOIN publishers AS p
ON a.city = p.city
ORDER BY a.au_lname DESCThis inner join is known as an equi-join. It returns all the columns in both tables, and returns only the rows for which there is an equal value in the join column.Here is the result set:au_id au_lname au_fname phone address city
----------- -------- -------- ------------ --------------- --------
238-95-7766 Carson Cheryl 415 548-7723 589 Darwin Ln. Berkeley
409-56-7008 Bennet Abraham 415 658-9932 6223 Bateman St. Berkeleystate zip contract pub_id pub_name city state country
----- ----- -------- ------ --------------------- -------- ----- -------
CA 94705 1 1389 Algodata Infosystems Berkeley CA USA
CA 94705 1 1389 Algodata Infosystems Berkeley CA USA (2 row(s) affected)
内联接是用比较运算符比较要联接列的值的联接。
---------------------------
使用内联接
在 SQL-92 标准中,内联接可在 FROM 或 WHERE 子句中指定。这是 WHERE 子句中唯一一种 SQL-92 支持的联接类型。WHERE 子句中指定的内联接称为旧式内联接。下面的 Transact-SQL 查询是内联接的一个示例:USE pubs
SELECT *
FROM authors AS a INNER JOIN publishers AS p
ON a.city = p.city
ORDER BY a.au_lname DESC此内联接称为相等联接。它返回两个表中的所有列,但只返回在联接列中具有相等值的行。下面是结果集:au_id au_lname au_fname phone address city
----------- -------- -------- ------------ --------------- --------
238-95-7766 Carson Cheryl 415 548-7723 589 Darwin Ln. Berkeley
409-56-7008 Bennet Abraham 415 658-9932 6223 Bateman St. Berkeleystate zip contract pub_id pub_name city state country
----- ----- -------- ------ --------------------- -------- ----- -------
CA 94705 1 1389 Algodata Infosystems Berkeley CA USA
CA 94705 1 1389 Algodata Infosystems Berkeley CA USA (2 row(s) affected)在结果集中,city 列出现两次。由于重复相同的信息没有意义,因此可以通过更改选择列表消除两个相同列中的一个。其结果称为自然联接。可以重新表述前面的 Transact-SQL 查询以形成自然联接。例如:USE pubs
SELECT p.pub_id, p.pub_name, p.state, a.*
FROM publishers p INNER JOIN authors a
ON p.city = a.city
ORDER BY a.au_lname ASC, a.au_fname ASC下面是结果集:pub_id pub_name state au_id au_lname au_fname
------ --------------- -------- ----------- -------- -------- 1389 Algodata Infosystems CA 409-56-7008 Bennet Abraham
1389 Algodata Infosystems CA 238-95-7766 Carson Cherylphone address city state zip contract
--------------- ------------- -------- ----- ----- ---------
415 658-9932 6223 Bateman St. Berkeley CA 94705 1
415 548-7723 589 Darwin Ln. Berkeley CA 94705 1(2 row(s) affected)本示例中,publishers.city 没有出现在结果中。