当前位置: 首页 > 图文教程 > 数据库 > MSSQL > SQL 外链接操作小结 inner join left join right join

MSSQL
MS SQL Server 2000系统数据类型
SQL Server几个容易出错的数据类型
SQL Server 数据库中关于死锁的分析
站长必备的sql查询语言基础知识
经验分享交流:常用SQL语句技法
SQL SERVER 2000 数据库备份与还原
解决SQL SERVER 2005无法远程连接的问题
SQL Server 安装参考意见
在sqlserver2005中安装sql server 2000的示例数据库northwind
SQL Server 2000 数据库分离与附加
高级自定义查询、分页、多表联合存储过程
SQL Server数据库下教你如何做导库SQL
常用的 MSSQL Server 数据修复命令
SQL存储过程初探
SQL Server存储过程编写经验和优化
卸载SQL Server2000后不能再次安装的问题解决方法
教你安装SQL Server 2005示例数据库
MySQL 的外键与参照完整性: Part 1
SQL Server安装:"安装文件配置服务器失败"的解决方法
SQL Server 数据库文件存放在何处

MSSQL 中的 SQL 外链接操作小结 inner join left join right join


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 60 ::
收藏到网摘: n/a

简介:
外部连接和自联接
inner join(等值连接) 只返回两个表中联结字段相等的行
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
on 指定表间联结字段及其关系的等号 "=" 表达式, 返回 true 或 false. 当表达式返回 true 时, 则查询中包含该记录.
! 外部连接只能操作已存在于数据库中的数据

Update (ctarticle AS a LEFT JOIN ctclass AS c ON a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid
SET tag=tag+' ', b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
Where a.classid=23 AND a.nclassid=0 AND tagid is not null

Update (ctarticle AS a LEFT JOIN (ctnclass AS c LEFT JOIN ctclass AS d ON c.classid = d.classid) ON a.nclassid = c.nclassid AND a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid SET tag=d.class+' '+c.nclass, b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid Where a.classid=23 AND a.nclassid=197;

更新操作
左连接中数据的筛选
Insert INTO cttag(articleid,classid,nclassid) Select a.articleid,a.classid,a.nclassid from ctarticle a left join cttag b on a.articleid=b.articleid where b.articleid is null

//本语句功能为, 显示主表的全部内容, 插入数据到副表中没有的数据
//主要作用为: 让数据减少冗余

上例中的延续
Select a.*, b.*, c.*, d.*
FROM cttag as d left join ((ctarticle AS a LEFT JOIN ctclass AS b ON a.classid=b.classid) LEFT JOIN ctnclass AS c ON a.nclassid=c.nclassid) on d.articleid=a.articleid;

显示文章表中的全部, 调用类别表中的栏目
select a.*, b.*, c.* from (ctarticle a left join ctclass b on a.classid=b.classid) left join ctnclass c on a.nclassid=c.nclassid

//作用, 有时在文章表中包含了在个别类别表中没有的数据, 用这个语法可以读出文章表的全部数据
//a 为 文章表, b 为主类别, c 为子类别

同上例, 选择追加数据时加上空格
Insert INTO cttag(articleid,classid,nclassid,tag)
Select a.articleid,a.classid,a.nclassid,d.class+' '+c.nclass
FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=4 and a.nclassid=154;

连接N个表, 并追加数据到其中一个表, N=4
insert INTO cttag(articleid,classid,nclassid,tag)
Select a.articleid,a.classid,a.nclassid,d.class+c.nclass
FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;

//解读
插入到 表2(栏1,栏2,栏3,栏4)
选择 别名a.栏1, 别名a.栏2, 别名a.栏3, 别名d.栏4 加上 别名c.栏5
从 (表1 别名a 左连接 (表3 别名c 左连接 表4 别名d 在 别名c.栏2 等于 别名d.栏2) 在 别名a.栏2 等于 别名c.栏2 和 别名a.栏3=别名c.栏3) 左连接 表2 别名b 在 别名a.栏1 等于 别名b.栏1 在那里 别名a.栏2=1 和 别名a.栏3=1

连接两个表, 并追加数据到其中一个表
Insert INTO cttag(articleid,classid,nclassid)
Select a.articleid,a.classid,a.nclassid
FROM ctarticle AS a LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;

//解读
插入到 表2(栏1,栏2,栏3)
选择 别名a.栏1, 别名a.栏2, 别名a.栏3
从 表1 别名a 左连接 表2 别名b 在 别名a.栏1 等于 别名b.栏1 在那里 别名a.栏4=1 和 别名a.栏5=1

左连接

同步两表的数据
Update ctarticle a INNER JOIN cttag b ON a.articleid = b.articleid SET b.classid=a.classid, b.nclassid=a.nclassid;

//解读
更新 表1 别名a 联接 表2 别名2 在 别名a.栏1 等于 别名b.栏1 设置 别名b.栏2 更新为 别名a.栏2, 别名b.栏3 更新为 别名a.栏3

右外连接
select a.*, b.* from bunclass a right join ctclass b on a.classid=b.classid where a.nclassid=20

查询别名 a,b 表, 只匹配 b 表中的内容.

添加数据到连接表之一
Insert INTO cttag ( tag, articleid ) Select top 1 b.tag, a.articleid FROM ctarticle AS a le