如何在sql server中连接两个表,它只提供不常见的记录 [英] How to join two tables in sql server that it only gives uncommon records

查看:92
本文介绍了如何在sql server中连接两个表,它只提供不常见的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入两个表,我只需要在两个表中都不常见的记录..需要使用哪个连接?或者如何做到这一点????

I want to join two tables where i need only records which are not common in both table.. which join need to be used??? or how this can be done????

推荐答案

检查此链接中的外部排除加入



SQL联接的可视化表示 [ ^ ]
check 'Outer Excluding JOIN' in this link

Visual Representation of SQL Joins[^]


让我们说第一张桌子A和第二张桌子B,



选择*

来自A full outer join B

on A. [user id] = B. [userid]

其中A. [userid]为null或B. [userid]为null



或者更好地看结果:



选择A. [用户ID],A。[用户名],A。[名字],A。[姓氏],B。[orderid],B。[ordername]

来自A左边加入B

on A. [user id] = B. [userid]

其中B. [userid]为空

UNION ALL

选择B. [用户ID],A。[用户名],A。[名字] ,A。[lastna我,B。[orderid],B。[ordername]
来自B的
左边加入A

在B. [user id] = A. [userid]

其中A. [userid]为空
lets say first table A and second table B,

select *
from A full outer join B
on A.[user id] = B.[userid]
where A.[userid] is null or B.[userid] is null

Or to see the result better:

select A.[userid], A.[username], A.[first name], A.[lastname], B.[orderid], B.[ordername]
from A left join B
on A.[user id] = B.[userid]
where B.[userid] is null
UNION ALL
select B.[userid], A.[username], A.[first name], A.[lastname], B.[orderid], B.[ordername]
from B left join A
on B.[user id] = A.[userid]
where A.[userid] is null


这篇关于如何在sql server中连接两个表,它只提供不常见的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆