Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第166题(2020-03-05):数据库的内连接和外连接有什么区别? #168

Open
qappleh opened this issue Mar 5, 2020 · 1 comment

Comments

@qappleh
Copy link
Owner

qappleh commented Mar 5, 2020

No description provided.

@qappleh
Copy link
Owner Author

qappleh commented Jul 3, 2020

数据库内连接外连接区别

(一)内连接
  内连接查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的列值。

内连接分三种:
  1、等值连接:在连接条件中使用等于号(=)运算符比较被连接列的列值,其查询结果中列出被连接表中的所有列,包括其中的重复列。
  例,下面使用等值连接列出authors和publishers表中位于同一城市的作者和出版社:

  SELECT *
  FROM authors AS a INNER JOIN publishers AS p
  ON a.city=p.city

  2、不等连接:在连接条件使用除等于运算符以外的其它比较运算符比较被连接的
列的列值。这些运算符包括>、>=、<=、<、!>、!<和<>。
  3、自然连接:在连接条件中使用等于(=)运算符比较被连接列的列值,但它使用选择列表指出查询结果集合中所包括的列,并删除连接表中的重复列。
  例,在选择列表中删除authors 和publishers 表中重复列(city和state):

  SELECTa.*,p.pub_id,p.pub_name,p.country
  FROM authors AS a INNER JOIN publishers AS p
  ON a.city=p.city

(二) 外连接  
  外连接,返回到查询结果集合中的不仅包含符合连接条件的行,而且还包括左表(左外连接或左连接))、右表(右外连接或右连接)或两个边接表(全外连接)中的所有数据行。
  left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录;
  right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录;
例如1:

SELECT a.*,b.* FROM luntanLEFT JOIN usertable as b
   ON a.username=b.username

例如2:

SELECT a.*,b.*
   FROM city as a FULL OUTER JOIN user asb
   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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant