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数据库的基本操作(增删改查),以及一些高级操作(视图.触发器.函数.存储过程等). 优化阶段:如何提高数据库的效率,如索引,分表等. 部署阶段:如何搭建真实 ...
随机推荐
- [Z]修炼成C++高手必看的C++书单
增添于网上的一些书单: C++/OPP/OOD系列: 层级一:语法/语意(C++) [Lippman2000] Essential C++ Essential C++,by Stanley B. Li ...
- iOS - 转场动画
苹果在 iOS7 定制了 ViewController 的切换效果 一 在iOS5和iOS6之前,ViewController的切换主要有4种 Push/Pop,NavigationViewCotnr ...
- ngingx安装错误 ./configure: error: the HTTP rewrite module requires the PCRE library.
有时候,我们需要单独安装nginx,来处理大量的下载请求.单独在Centos5安装nginx遇到的rewrite和HTTP cache错误解决办法: wget http://nginx.org/do ...
- SQL Server2008安装后1433端口没监听问题
win2008系统安装完SQL Server2008后发现1433端口并没有监听,netstat -an并没有发现监听的1433端口,本机telnet localhost 1433也连不通,百度之后说 ...
- Android studio Unable to start the daemon process
Unable to start the daemon process.This problem might be caused by incorrect configuration of the da ...
- jQuery mobile 初始化页面的过程
- js ==和===以及!= 和 !==的区别
一.js == 与 === 的区别[转] 1. 对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型 ...
- mysql客户端工具
MySQL 数据库不仅提供了数据库的服务器端应用程序,同时还提供了大量的客户端工具程序,如 mysql,mysqladmin,mysqldump 等等,都是大家所熟悉的.虽然有些人对这些工具的功能都已 ...
- yum安装pip,pip安装compose
#centos7 yum -y install epel-release yum -y install python-pip pip install --upgrade pip pip install ...
- 【转】详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C#等)
转自:http://www.crifan.com/files/doc/docbook/web_scrape_emulate_login/release/html/web_scrape_emulate_ ...