连接查询

连接查询:列的扩展
1. 等值连接查询 (内连接)
笛卡尔积
join on

2. 外连接(left.right.full)

left左连接:
left 左边表为主表 ,右边的为从表,显示的结果集以主表为主
如果从表中没有和它匹配的数据,就以NULL值填充

right右连接:
right 右边的表为主表

full 全连接
每个表都完全显示,互相匹配数据,如果对方没有则以NULL填充

联合查询:行的扩展

用Union将两个查询联合起来显示,追加行的形式在结果集中显示
必须保证两个查询结果的列的格式是一样的

子查询

1.无关子查询:查询嵌套,外层查询成为主查询,内层查询成为子查询
子查询的结果成为主查询的条件

select Info.Code,Info.Name,Nation.Name from Info,Nation where Nation.Code = Info.Nation

SELECT Info.Code,Info.Name,Nation.Name,Family.Name from Info JOIN Nation on Nation.Code = Info.Nation JOIN Family on Info.Code=Family.InfoCode

SELECT Info.Code,Info.Name,Nation.Name from Info right JOIN Nation on Nation.Code = Info.Nation

SELECT Info.Code,Info.Name,Nation.Name from Info full JOIN Nation on Nation.Code = Info.Nation


SELECT Code,Name from Info
UNION
SELECT code,Name from Nation

select * FROM Info where YEAR(Birthday) = (SELECT YEAR(Birthday) from Info where Code = 'p005')

SELECT TOP 5 * from News where ids NOT IN (SELECT TOP 10 ids FROM News)

SELECT * from score where degree>ALL(SELECT degree from score where sno='105')

 

posted on 2015-05-27 21:00  rongzichuan  阅读(185)  评论(0编辑  收藏  举报