请教一个基础问题...

gerrylc 2005-11-30 05:40:16
请解释 Inner join on 是怎么用的,最好可以举个简单的例子,谢了
...全文
69 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gerrylc 2005-12-01
  • 打赏
  • 举报
回复
多谢各位...
jin2005 2005-11-30
  • 打赏
  • 举报
回复
具体请看一下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. Berkeley

state 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 Cheryl

phone 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 没有出现在结果中。
gerrylc 2005-11-30
  • 打赏
  • 举报
回复
请具体解释一下,谢了
yinwun 2005-11-30
  • 打赏
  • 举报
回复
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 DESC

This 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. Berkeley

state 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)

raohui_1978 2005-11-30
  • 打赏
  • 举报
回复
select a.* from table1 a inner join table2 b on a.id=b.id

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧