惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - begincsdn

C# 自定义光标使用的常见方法 二叉树的递归遍历以及非递归遍历(一)----先序遍历(转) Pentium指令的执行周期数[转] 计算机图形学 -- 光栅图形学扫描线填充多边形[转] 推荐一个跨平台内存分配器【转载】 几何图形库-GEOS使用示例(1) 开源几何计算数学库 C++中固定长度短字符串比较是否相同,忽略大小写比对时的小技巧 二维图形变换公式推导---------旋转变换 .NET 漫淡(一) --- 需要充分认识的应用程序域-AppDomain Gerber 文件格式(一):RS-274X 语法 SCRUM 系列之一 ----- 认识SRCUM GERBER文件格式简介 迷思微软两大框架的RTTI(Run-Time Type Identification) Remoting中,关于获取某接口派生的自定义属性(CustomAttribute)的问题 让你的VS。NET2003可以调试ASP服务器脚本。 欢迎大家加入我申请的QQ群。 客户端独立弹出详细的实现过程 - begincsdn - 博客园 Microsoft.Visual.Studio..NET.2005.预发行版 EMULE下载
UNION,EXISTS,IN等在SQL语句中的灵活应用和场境的选择。
begincsdn · 2005-11-22 · via 博客园 - begincsdn

今天在QQ群里,有朋友问我这样的一个SQL Server中查询数据的问题,
表 A:
num       unit
001        a
002        b
003        c

表 B:
num      unit
001        d


查询显示结果
001     d
002     b
003     c

怎么实现?

看了这个问题第一感觉是,有点难度。
不过要表达的意思很明白:匹配的显示b,不匹配显示a.

问题来了,简单的SQL语句中无法找到这样的查询方式能完成这项工作,不回答别人吧,又难受。说不会,显得很没面子。怎么办呢?

于是以最短的时间分析了下,发现我可以分成两个查询两完成工作。
即:SELECT a.num, b.unit
FROM a INNER JOIN
      b ON a.num = b.num

SELECT DISTINCT a.num, a.unit
FROM a WHERE a.num NOT IN
          (SELECT b.num
         FROM b)
而清楚的记得union可以将两个集合合并,只要集合的字段数目和类型是一样的。
那就好办,于是写下:
SELECT a.num, b.unit
FROM a INNER JOIN
      b ON a.num = b.num
UNION
SELECT DISTINCT a.num, a.unit
FROM a WHERE a.num NOT IN
          (SELECT b.num
         FROM b)

测试执行,搞定。

如果字段多,写起来麻烦,想使用*,则部分数据冗余的写法是:
SELECT a.*, b.unit AS extend
FROM a INNER JOIN
      b ON a.num = b.num
UNION
SELECT DISTINCT a.*, a.unit AS extend
FROM a WHERE a.num NOT IN
          (SELECT b.num
         FROM b)

本以为问题就这样解决了,但问题又来了,原因是这对单个字段是唯一键或主键时有效,如果唯一键是两个或两个以上的字段组成就不行了。

表 A:
num    num2   unit
001       a      a
002       b      b
003       c      c

表 B:
num   num2   unit
001      a        d

查询显示结果
num num1 unit
001    a   d
002    b   b
003    c   c

于是改写成
SELECT a.*, b.unit AS extend
FROM a INNER JOIN
      b ON a.num = b.num and a.num1 = b.num1
UNION
SELECT DISTINCT a.*, a.unit AS extend
FROM a
WHERE not exists 
(SELECT * from b where a.num=b.num and a.num1=b.num1)