请教Sql问题:关于on和inner join!(50分)

  • 主题发起人 主题发起人 puremoonstone
  • 开始时间 开始时间
P

puremoonstone

Unregistered / Unconfirmed
GUEST, unregistred user!
sql server上有这样一段例程,我读得不大懂,想请教大家:
USE pubs

SELECT ytd_sales AS Sales,

authors.au_fname + ' '+ authors.au_lname AS Author,
ToAuthor = (ytd_sales * royalty) / 100,
ToPublisher = ytd_sales - (ytd_sales * royalty) / 100
FROM titles INNER JOIN titleauthor
ON titles.title_id = titleauthor.title_id INNER JOIN authors
ON titleauthor.au_id = authors.au_id
ORDER BY Sales DESC, Author ASC
请问on和inner join的作用是什么,该怎么用?
 
其实是一种规范的写法:好处是能避免表扫描,结构清晰
ON titles.title_id = titleauthor.title_id
表示titleauthor 和 titles 这两个表的title_id 字段相同,一般是外键
 
连接查询
通过连接运算符可以实现多个表查询。连接是关系数据库模型的主要特点,也是它区别于其它类型
数据库管理系统的一个标志。
在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在
一个表中。当检索数据时,通过连接操作查询出存放在多个表中的不同实体的信息。连接操作给用户带
来很大的灵活性,他们可以在任何时候增加新的数据类型。为不同实体创建新的表,尔后通过连接进行
查询。
连接可以在SELECT 语句的FROM子句或WHERE子句中建立,似是而非在FROM子句中指出连接时有助于
将连接操作与WHERE子句中的搜索条件区分开来。所以,在Transact-SQL中推荐使用这种方法。
SQL-92标准所定义的FROM子句的连接语法格式为:
FROM join_table join_type join_table
[ON (join_condition)]
其中join_table指出参与连接操作的表名,连接可以对同一个表操作,也可以对多表操作,对同一
个表操作的连接又称做自连接。
join_type 指出连接类型,可分为三种:内连接、外连接和交叉连接。内连接(INNER JOIN)使用比
较运算符进行表间某(些)列数据的比较操作,并列出这些表中与连接条件相匹配的数据行。根据所使用
的比较方式不同,内连接又分为等值连接、自然连接和不等连接三种。
外连接分为左外连接(LEFT OUTER JOIN或LEFT JOIN)、右外连接(RIGHT OUTER JOIN或RIGHT JOIN)
和全外连接(FULL OUTER JOIN或FULL JOIN)三种。与内连接不同的是,外连接不只列出与连接条件相匹
配的行,而是列出左表(左外连接时)、右表(右外连接时)或两个表(全外连接时)中所有符合搜索条件的
数据行。
交叉连接(CROSS JOIN)没有WHERE 子句,它返回连接表中所有数据行的笛卡尔积,其结果集合中的
数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。
连接操作中的ON (join_condition) 子句指出连接条件,它由被连接表中的列和比较运算符、逻辑
运算符等构成。
无论哪种连接都不能对text、ntext和image数据类型列进行直接连接,但可以对这三种列进行间接
连接。例如:
SELECT p1.pub_id,p2.pub_id,p1.pr_info
FROM pub_info AS p1 INNER JOIN pub_info AS p2
ON DATALENGTH(p1.pr_info)=DATALENGTH(p2.pr_info)
(一)内连接
内连接查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的列值。内连接分
三种:
1、等值连接:在连接条件中使用等于号(=)运算符比较被连接列的列值,其查询结果中列出被连接
表中的所有列,包括其中的重复列。
2、不等连接: 在连接条件使用除等于运算符以外的其它比较运算符比较被连接的列的列值。这些
运算符包括>、>=、<=、<、!>、!<和<>。
3、自然连接:在连接条件中使用等于(=)运算符比较被连接列的列值,但它使用选择列表指出查询
结果集合中所包括的列,并删除连接表中的重复列。
例,下面使用等值连接列出authors和publishers表中位于同一城市的作者和出版社:
SELECT *
FROM authors AS a INNER JOIN publishers AS p
ON a.city=p.city
又如使用自然连接,在选择列表中删除authors 和publishers 表中重复列(city和state):
SELECT a.*,p.pub_id,p.pub_name,p.country
FROM authors AS a INNER JOIN publishers AS p
ON a.city=p.city
(二)外连接
内连接时,返回查询结果集合中的仅是符合查询条件( WHERE 搜索条件或 HAVING 条件)和连接条件
的行。而采用外连接时,它返回到查询结果集合中的不仅包含符合连接条件的行,而且还包括左表(左外
连接时)、右表(右外连接时)或两个边接表(全外连接)中的所有数据行。
如下面使用左外连接将论坛内容和作者信息连接起来:
SELECT a.*,b.* FROM luntan LEFT JOIN usertable as b
ON a.username=b.username
下面使用全外连接将city表中的所有作者以及user表中的所有作者,以及他们所在的城市:
SELECT a.*,b.*
FROM city as a FULL OUTER JOIN user as b
ON a.username=b.username
(三)交叉连接
交叉连接不带WHERE 子句,它返回被连接的两个表所有数据行的笛卡尔积,返回到结果集合中的数
据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。
例,titles表中有6类图书,而publishers表中有8家出版社,则下列交叉连接检索到的记录数将等
于6*8=48行。
SELECT type,pub_name
FROM titles CROSS JOIN publishers
ORDER BY type
 
谢谢yaya8163、sportsman!
再请问:左连接和右连接指的是什么(是不是指根据哪个表来匹配,我不大清楚)?
请给出例子和查询结果。
再次感谢yaya8163的详细解答!
 
to yaya8163:请回答。
 
to:yaya8163 哇,你那么详细,真敬业啊,我也试试:有不对之处,批评我,千万别保留。
to:puremoonstone
假设有这样两个表:
luntan:
-------------------------
username message
aa 1111111
bb 2222222
cc 3333333

usertable:
------------------------
username age sex
aa 22 男
tt 33 女
cc 44 男
gg 55 女

对于左外连接:
SELECT a.*,b.*
FROM luntan LEFT JOIN usertable as b
ON a.username=b.username

则查询的结果为:
---------------------------------------
username message age sex
aa 1111111 22 男
bb NULL NULL NULL
cc 333333 44 男

对于右外连接:
SELECT a.*,b.*
FROM luntan RIGHT JOIN usertable as b
ON a.username=b.username

则查询的结果为:
---------------------------------------
username message age sex
aa 1111111 22 男
tt NULL 33 女
cc 333333 44 男
gg NULL 55 女


---Arewen---
 
to arewen:谢谢您的例子。您有关于左连接、右连接的解释吗?我想知道怎样计算(公式)。
谢谢!
 
Using Outer Joins
Inner joins return rows only when there is at least one row from both tables that matches the join condition. Inner joins eliminate the rows that do not match with a row from the other table. Outer joins, however, return all rows from at least one of the tables or views mentioned in the FROM clause, as long as those rows meet any WHERE or HAVING search conditions. All rows are retrieved from the left table referenced with a left outer join, and all rows from the right table referenced in a right outer join. All rows from both tables are returned in a full outer join

Microsoft&amp;reg; SQL Server&amp;#8482; uses these SQL-92 keywords for outer joins specified in a FROM clause:

LEFT OUTER JOIN or LEFT JOIN
RIGHT OUTER JOIN or RIGHT JOIN
FULL OUTER JOIN or FULL JOIN
SQL Server supports both the SQL-92 outer join syntax and a legacy syntax for specifying outer joins based on using the *= and =* operators in the WHERE clause. The SQL-92 syntax is recommended because it is not subject to the ambiguity that sometimes results from the legacy Transact-SQL outer joins.

Using Left Outer Joins
Consider a join of the authors table and the publishers table on their city columns. The results show only the authors who live in cities in which a publisher is located (in this case, Abraham Bennet and Cheryl Carson).

To include all authors in the results, regardless of whether a publisher is located in the same city, use an SQL-92 left outer join. Here is the query and results of the Transact-SQL left outer join:

USE pubs

SELECT a.au_fname, a.au_lname, p.pub_name

FROM authors a LEFT OUTER JOIN publishers p

ON a.city = p.city

ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC



Here is the result set:

au_fname au_lname pub_name

-------------------- ------------------------------ -----------------

Reginald Blotchet-Halls NULL

Michel DeFrance NULL

Innes del Castillo NULL

Ann Dull NULL

Marjorie Green NULL

Morningstar Greene NULL

Burt Gringlesby NULL

Sheryl Hunter NULL

Livia Karsen NULL

Charlene Locksley NULL

Stearns MacFeather NULL

Heather McBadden NULL

Michael O'Leary NULL

Sylvia Panteley NULL

Albert Ringer NULL

Anne Ringer NULL

Meander Smith NULL

Dean Straight NULL

Dirk Stringer NULL

Johnson White NULL

Akiko Yokomoto NULL

Abraham Bennet Algodata Infosystems

Cheryl Carson Algodata Infosystems



(23 row(s) affected)



The LEFT OUTER JOIN includes all rows in the authors table in the results, whether or not there is a match on the city column in the publishers table. Notice that in the results there is no matching data for most of the authors listed; therefore, these rows contain null values in the pub_name column.

Using Right Outer Joins
Consider a join of the authors table and the publishers table on their city columns. The results show only the authors who live in cities where a publisher is located (in this case, Abraham Bennet and Cheryl Carson). The SQL-92 right outer join operator, RIGHT OUTER JOIN, indicates all rows in the second table are to be included in the results, regardless of whether there is matching data in the first table.

To include all publishers in the results, regardless of whether a city has a publisher located in the same city, use an SQL-92 right outer join. Here is the Transact-SQL query and results of the right outer join:

USE pubs

SELECT a.au_fname, a.au_lname, p.pub_name

FROM authors AS a RIGHT OUTER JOIN publishers AS p
ON a.city = p.city

ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC



Here is the result set:

au_fname au_lname pub_name

-------------------- ------------------------ ----------

Abraham Bennet Algodata Infosystems

Cheryl Carson Algodata Infosystems

NULL NULL Binnet &amp; Hardley

NULL NULL Five Lakes Publishing

NULL NULL GGG&amp;G

NULL NULL Lucerne Publishing

NULL NULL New Moon Books

NULL NULL Ramona Publishers

NULL NULL Scootney Books



(9 row(s) affected)



An outer join can be further restricted by using a predicate (such as comparing the join to a constant). This example contains the same right outer join, but eliminates all titles that have sold less than 50 copies:

USE pubs

SELECT s.stor_id, s.qty, t.title

FROM sales s RIGHT OUTER JOIN titles t

ON s.title_id = t.title_id

AND s.qty > 50

ORDER BY s.stor_id ASC



Here is the result set:

stor_id qty title

------- ------ ---------------------------------------------------------

(null) (null) But Is It User Friendly?

(null) (null) Computer Phobic AND Non-Phobic Individuals: Behavior

Variations

(null) (null) Cooking with Computers: Surreptitious Balance Sheets

(null) (null) Emotional Security: A New Algorithm

(null) (null) Fifty Years in Buckingham Palace Kitchens

7066 75 Is Anger the Enemy?

(null) (null) Life Without Fear

(null) (null) Net Etiquette

(null) (null) Onions, Leeks, and Garlic: Cooking Secrets of the

Mediterranean

(null) (null) Prolonged Data Deprivation: Four Case Studies

(null) (null) Secrets of Silicon Valley

(null) (null) Silicon Valley Gastronomic Treats

(null) (null) Straight Talk About Computers

(null) (null) Sushi, Anyone?

(null) (null) The Busy Executive's Database Guide

(null) (null) The Gourmet Microwave

(null) (null) The Psychology of Computer Cooking

(null) (null) You Can Combat Computer Stress!



(18 row(s) affected)



For more information about predicates, see WHERE.

Using Full Outer Joins
To retain the nonmatching information by including nonmatching rows in the results of a join, use a full outer join. Microsoft&amp;reg; SQL Server&amp;#8482; provides the full outer join operator, FULL OUTER JOIN, which includes all rows from both tables, regardless of whether or not the other table has a matching value.

Consider a join of the authors table and the publishers table on their city columns. The results show only the authors who live in cities in which a publisher is located (in this case, Abraham Bennet and Cheryl Carson). The SQL-92 FULL OUTER JOIN operator indicates that all rows from both tables are to be included in the results, regardless of whether there is matching data in the tables.

To include all publishers and all authors in the results, regardless of whether a city has a publisher located in the same city, or whether a publisher is located in the same city, use a full outer join. Here is the query and results of the Transact-SQL full outer join:

USE pubs

SELECT a.au_fname, a.au_lname, p.pub_name

FROM authors a FULL OUTER JOIN publishers p
ON a.city = p.city

ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC



Here is the result set:

au_fname au_lname pub_name

-------------------- ---------------------------- --------------------

Reginald Blotchet-Halls NULL

Michel DeFrance NULL

Innes del Castillo NULL

Ann Dull NULL

Marjorie Green NULL

Morningstar Greene NULL

Burt Gringlesby NULL

Sheryl Hunter NULL

Livia Karsen NULL

Charlene Locksley NULL

Stearns MacFeather NULL

Heather McBadden NULL

Michael O'Leary NULL

Sylvia Panteley NULL

Albert Ringer NULL

Anne Ringer NULL

Meander Smith NULL

Dean Straight NULL

Dirk Stringer NULL

Johnson White NULL

Akiko Yokomoto NULL

Abraham Bennet Algodata Infosystems

Cheryl Carson Algodata Infosystems

NULL NULL Binnet &amp; Hardley

NULL NULL Five Lakes Publishing

NULL NULL GGG&amp;G

NULL NULL Lucerne Publishing

NULL NULL New Moon Books

NULL NULL Ramona Publishers

NULL NULL Scootney Books



(30 row(s) affected)

 
非常感谢arewen、sportsman、yaya8163的热心解答。我终于明白了。谢谢!
 
多人接受答案了。
 
我觉得对于左外连接例子中的:
username message age sex
aa 1111111 22 男
bb NULL NULL NULL
cc 333333 44 男
bb记录message应该位222222
 
后退
顶部