sql求两表的并集、交集、非交集、差集、结果集排序

create table A(
 id int IDENTITY(1,1) Not null primary key,
 name varchar(20) not null default(‘‘),
)
INSERT INTO [A]([name]) VALUES(‘a‘)
INSERT INTO [A]([name]) VALUES(‘b‘)
INSERT INTO [A]([name]) VALUES(‘c‘)
INSERT INTO [A]([name]) VALUES(‘d‘)
INSERT INTO [A]([name]) VALUES(‘e‘)
INSERT INTO [A]([name]) VALUES(‘f‘)
INSERT INTO [A]([name]) VALUES(‘g‘)

create table B(
 id int IDENTITY(1,1) Not null primary key,
 name varchar(20) not null default(‘‘),
)

INSERT INTO [B]([name]) VALUES(‘a‘)
INSERT INTO [B]([name]) VALUES(‘b‘)
INSERT INTO [B]([name]) VALUES(‘c‘)
INSERT INTO [B]([name]) VALUES(‘d‘)
INSERT INTO [B]([name]) VALUES(‘h‘)
INSERT INTO [B]([name]) VALUES(‘i‘)
INSERT INTO [B]([name]) VALUES(‘j‘)
SELECT * from A union select * from B  --查询A\B表的并集重复的项只显示一个
SELECT * from A union all select * from B ----查询A\B表的并集重复的也显示
SELECT * from A union all select * from B  order by id asc ---查询A\B表的并集重复的也显示,并按照id升序
SELECT * from A INTERSECT select * from B ----查询两表的交集
SELECT * from A EXCEPT select * from B ----查询A表中不与B表重复的记录
--查询A\B两表中所有非交集的记录
(SELECT * from A EXCEPT select * from B) union (SELECT * from B EXCEPT select * from A) 

原文地址:https://www.cnblogs.com/25miao/p/10611518.html

时间: 2024-08-29 09:41:53

sql求两表的并集、交集、非交集、差集、结果集排序的相关文章

sql语句 两表关联查询计算数量

select sum(a1.`num`)   from `order_orderlistrow` as a1 INNER JOIN `order_orderlist` as a2 on a1.`order_orderlist_id`  = a2.`id`  where a1.`goods_good_id` ='54' and a2.`state` <> '0' 原文地址:https://www.cnblogs.com/daochong/p/10281478.html

哈希(4) - 求两个链表的交集(intersection)以及并集(union)

给定两个链表,求它们的交集以及并集.用于输出的list中的元素顺序可不予考虑. 例子: 输入下面两个链表: list1: 10->15->4->20 list2: 8->4->2->10 输出链表: 交集list: 4->10 并集list: 2->8->20->4->15->10 方法1 (简单方法) 可以参考链表系列中的"链表操作 - 求两个链表的交集(intersection)以及并集(union)" 方法2

利用sort和uniq求两个文件的并集,交集和差集

利用sort和uniq求两个文件的并集,交集和差集 并集:cat file1.txt file2.txt | sort | uniq > file.txt 交集:cat file1.txt file2.txt | sort | uniq -d >file.txt 差集:求file1.txt相对于file2.txt的差集,可先求出两者的交集file3.txt,然后在file1.txt中除去file3.txt即可. cat file1.txt file2.txt | sort | uniq -d

求两个集合的交集和并集C#

我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace JiaoJi { class Program { static void Main(string[] args) { int [] arrA=new int[8]{1,2,3,4,5,6,7,8}; int [] arrB=new int[5]{4,5,

Python 求两个文本文件以行为单位的交集 并集 差集

Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r').readlines()) print 'ins: %s'%(s1.intersection(s2)) print 'uni: %s'%(s1.union(s2)) print 'dif: %s'%(s1.difference(s2).union(s2.difference(s1))) 原文地址:h

求两个数组的交集

问题: 给你两个排序的数组,求两个数组的交集. 比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5. 思路: 1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存.该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度. 2. 因为A B 都排过序,所以,每一次从B数组取值后,可以利用二分查找看是否在数组A里有B所对应的值,这样复杂度变成了O(N lg M). 这里,如果N 比 M 大,可以从A中取值,然后在B中判断是否有A

一条SQL语句查询两表中两个字段

首先描述问题,student表中有字段startID,endID.garde表中的ID需要对应student表中的startID或者student表中的endID才能查出grade表中的name字段,这时候问题就来了,如果需要一条sql一句同时查出garde表中的两条数据怎么办?(两表的关联字段为 SID) sql="select b.name,c.name as name2 from student a,garde b,grade c where a.SID=b.SID and a.SID=c

Java求两个List的交集

1 package demo; 2 3 import java.util.List; 4 5 public class Demo { 6 7 @SuppressWarnings("unchecked") 8 public static void main(String[] args) { 9 List array1=new ArrayList(); 10 array1.add("1");array1.add("2"); 11 List array

SQL两表关联查询&批量修改字段值

SQL关联查询&修改字段,正确范例如下: --批量修改报告单位名称&更新时间 --tt和tp两表关联查询,将符合条件的tt表中的principal字段更新到tp表的ruperson字段 merge into nhis34.t_publicplaces tp using standard.t_organization tt on (tt.orgcode = tp.r_orgcode and tp.create_time > '2015-05-07 00:00:00') when mat