SQL实现交,并,差操作
mysql> desc a;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| tel | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec) mysql> desc b;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| tel | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
a表和b表中的数据不同,现在要查询a表和b表的电话号码的交,并,差集:
mysql> select * from a;
+----+------+
| id | tel |
+----+------+
| 1 | 1 |
| 2 | 2 |
+----+------+
2 rows in set (0.00 sec) mysql> select * from b;
+----+------+
| id | tel |
+----+------+
| 1 | 2 |
| 2 | 3 |
+----+------+
2 rows in set (0.01 sec)
交集
//联合查询
mysql> select a.tel from a,b where a.tel = b.tel;
+------+
| tel |
+------+
| 2 |
+------+
1 row in set (0.00 sec) //也可以使用嵌套查询
mysql> select a.tel from a where a.tel in (select b.tel from b);
+------+
| tel |
+------+
| 2 |
+------+
1 row in set (0.00 sec) //也可以
mysql> select a.tel from a where exists (select * from b where a.tel = b.tel);
+------+
| tel |
+------+
| 2 |
+------+ //当然intersect有可能是不支持的
mysql> select a.tel from a intersect select b.tel from b;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select b.tel from b' at line
并集
//MySQL从4.0以后是支持union,union all的,其实实现union是非常复杂的
mysql> select a.tel from a union select b.tel from b;
+------+
| tel |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec) //
差集
except所代表的差集,是在a中出现但是不在b中出现的记录;
mysql> select a.tel from a where a.tel not in ( select b.tel from b);
+------+
| tel |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
//同样,except也不一定支持
mysql> select a.tel from a except select b.tel form b;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select b.tel form b' at line
SQL实现交,并,差操作的更多相关文章
- Java集合set的并、交、差操作
集合的并.交.差操作 Set<Integer> result = new HashSet<Integer>(); Set<Integer> set1 = new H ...
- 用SQL表达交并差操作
交-并-差的处理 SQL语言:并运算UNION,交运算INTERSECT,差运算EXCEPT 基本语法形式: 子查询{UNION [ALL] | INTERSECT [ALL] | EXPECT [A ...
- (转)SQL对Xml字段的操作
T-Sql操作Xml数据 一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和 ...
- SQL对Xml字段的操作
转:http://www.cnblogs.com/youring2/archive/2008/11/27/1342288.html T-Sql操作Xml数据 一.前言 SQL Server 2005 ...
- STL中的set集合容器进行集合运算:并、交、差实例
集合容器的集合运算:并.交.差: #include "stdafx.h" #include <iostream> #include <set> #inclu ...
- 对于超大型SQL SERVER数据库执行DBCC操作
原文:对于超大型SQL SERVER数据库执行DBCC操作 对于数据库维护,主要使用DBCC CHECKDB来实现,以下是对大型数据库的使用说明,小型数据库一般直接使用就可以了: 1.2008(200 ...
- SQL导入txt以及SQL中的时间格式操作
原文:SQL导入txt以及SQL中的时间格式操作 MySQL中导入txt的指令为: load data local infile "路径名称" into table "表 ...
- SQL点滴2—重温sql语句中的join操作
原文:SQL点滴2-重温sql语句中的join操作 1.join语句 Sql join语句用来合并两个或多个表中的记录.ANSI标准SQL语句中有四种JOIN:INNER,OUTER,LEFTER,R ...
- Linq to SQL 简单的增删改操作
Linq to SQL 简单的增删改操作. 新建数据库表tbGuestBook.结构如下: 新建web项目,完成相应的dbml文件.留言页面布局如下 <body> <form id= ...
- MySQL(一) -- MySQL学习路线、数据库的基础、关系型数据库、关键字说明、SQL、MySQL数据库、MySQL服务器对象、SQL的基本操作、库操作、表操作、数据操作、中文数据问题、 校对集问题、web乱码问题
1 MySQL学习路线 基础阶段:MySQL数据库的基本操作(增删改查),以及一些高级操作(视图.触发器.函数.存储过程等). 优化阶段:如何提高数据库的效率,如索引,分表等. 部署阶段:如何搭建真实 ...
随机推荐
- 通过java的i/o机制进行图片流的存储以及对网络图片的存储
存储内地图片思路:首先把原有的图片以流的方式读取出来,再以流的方式存储到目标文件: package imgStream; import java.io.*; public class ImgStrea ...
- 获取访客IP、地区位置信息、浏览器、来源页面
<?php //这个类似用来获取访客信息的 //方便统计 class visitorInfo { //获取访客ip public function getIp() { $ip=false; if ...
- ANDROID – 單色漸層效果的改良 – GRADIENT SCRIMS(转)
本篇是根據 +Roman Nurik 在 2014/11/24 發佈的一篇 G+ 而來.看到他發文後,起了好奇心,就根據他提出的方法嘗試著實作,並將之排列呈現,直接從視覺上做個比較. 他在 G+ 的發 ...
- 转载:C/C++关于string.h头文件和string类
学习C语言时,用字符串的函数例如stpcpy().strcat().strcmp()等,要包含头文件string.h 学习C++后,C++有字符串的标准类string,string类也有很多方法,用s ...
- jQuery事件处理(一)
1.jQuery事件绑定的用法: $( "elem" ).on( events, [selector], [data], handler ); events:事件名称,可以是自定义 ...
- Android 屏幕适配:最全面的解决方案
转自:https://www.jianshu.com/p/ec5a1a30694b 前言 Android的屏幕适配一直以来都在折磨着我们Android开发者,本文将结合: Google的官方权威适配文 ...
- sencha touch list(列表) item(单行)单击事件触发顺序
测试代码如下 Ext.define('app.view.new.List', { alternateClassName: 'newList', extend: 'app.view.util.MyLis ...
- [转]redhat7(centos7) not registered to Red Hat Subscription Management
[root@controller0 ~]# yum install ntp Loaded plugins: fastestmirror, product-id, search-disabled-rep ...
- rabbitmq日志记录进出的每条消息
参考: https://blog.csdn.net/u013256816/article/details/76039201 https://blog.csdn.net/aosica321/articl ...
- 查看运行中的Java其配置的堆大小
一.背景 有题目中的需求,也不是空穴来风:前一阵给公司搭建了一个持续集成服务器,Jenkins.最近发现,运行一段时间后,就变慢了. 随便一个操作,cpu就飙高了.然后就思考会不会是内存不够用,频繁G ...