mySQL UNION运算符的默认规则研究
发布时间:2024-01-21 17:51:39
/* 建立数据表 */
create table td_base_data( id int(10) not null auto_increment,userId int(10) default '0',primary key (`id`))ENGINE=MyISAM DEFAULT CHARSET=gbk;
create table td_base_data_20090527( id int(10) not null auto_increment,userId int(10) default '0',primary key (`id`))ENGINE=MyISAM DEFAULT CHARSET=gbk;
/* 插入模拟记录 */
insert into td_base_data(userId) values(1);
insert into td_base_data(userId) values(45);
insert into td_base_data(userId) values(45);
insert into td_base_data(userId) values(1);
insert into td_base_data(userId) values(45);
insert into td_base_data_20090527(userId) values(1);
insert into td_base_data_20090527(userId) values(45);
insert into td_base_data_20090527(userId) values(45);
insert into td_base_data_20090527(userId) values(1);
insert into td_base_data_20090527(userId) values(45);
insert into td_base_data_20090527(userId) values(45);
/* 查询测试 */
select count(userId) as cnumber from td_base_data where userId = '45';
/* 3 */
select count(userId) as cnumber from td_base_data_20090527 where userId = '45';
/* 4 */
select (select count(userId) from td_base_data where userId = '45') + (select count(userId) from td_base_data_20090527 where userId = '45') as cnumber;
/* 7 */
select count(*) from
(
select id from td_base_data where userId = '45'
union
select id from td_base_data_20090527 where userId = '45'
) as tx;
/* 4 */
select count(*) from
(
select * from td_base_data where userId = '45'
union
select * from td_base_data_20090527 where userId = '45'
) as tx;
/* 4 */
/* 证明在mysql中,union本身有剔除重复项的作用 */
/* 查询手册定义 */
/*
查询mysql参考手册:
13.2.7.2. UNION语法
如果您对UNION不使用关键词ALL,则所有返回的行都是唯一的,如同您已经对整个结果集合使用了DISTINCT。如果您指定了ALL,您会从所有用过的SELECT语句中得到所有匹配的行。
DISTINCT关键词是一个自选词,不起任何作用,但是根据SQL标准的要求,在语法中允许采用。(在MySQL中,DISTINCT代表一个共用体的默认工作性质。)
*/
/* 证明在mysql中,union默认就是DISTINCT的 */
/*
查询mssql参考手册:
Transact-SQL 参考
UNION 运算符:
使用 UNION 组合两个查询的结果集的两个基本规则是:
1.所有查询中的列数和列的顺序必须相同。
2.数据类型必须兼容。
参数:
UNION
指定组合多个结果集并将其作为单个结果集返回。
ALL
在结果中包含所有的行,包括重复行。如果没有指定,则删除重复行。
*/
/* 证明在mssql中,union默认也是DISTINCT的 */
/* 查询标准定义 */
/*
查询SQL2003标准:
Transact-SQL 参考
4.10.6.2 Operators that operate on multisets and return multisets
MULTISET UNION is an operator that computes the union of two multisets. There are two variants, specified using ALL or DISTINCT, to either retain duplicates or remove duplicates.
7.13 <query expression>
Syntax Rules
6) If UNION, EXCEPT, or INTERSECT is specified and neither ALL nor DISTINCT is specified, then DISTINCT is implicit.
*/
/* 可见SQL2003标准定义了DISTINCT就是union的默认值 */
/* 正确查询,同时应该在两表的userId字段上做索引以加快查询速度 */
select count(userId) as cnumber from
(
select userId from td_base_data where userId = '45'
union all
select userId from td_base_data_20090527 where userId = '45'
) as tx;
猜你喜欢
- 如何用SQLMail建立一个电子刊物自动处理系统?下面我们用SQLMail来做一个电子刊物自动处理系统。在这个系统中,主要实现两个功能:1、
- 在Python中,字典是通过散列表或说哈希表实现的。字典也被称为关联数组,还称为哈希数组等。也就是说,字典也是一个数组,但数组的索引是键经过
- 本文实例讲述了Python处理命令行参数模块optpars用法。分享给大家供大家参考,具体如下:optpars是python中用来处理命令行
- 需求最近公司干活,收到一个需求,说是让手动将数据库查出来的信息复制粘贴到excel中,在用excel中写好的公式将指定的两列数据用updat
- 如下所示:# Seed random number generatornp.random.seed(42)# Compute mean no
- 本文给出了本人在使用 Sublime Text 配置 Python 环境的记录。主要为了解决中文乱码的问题以及 Sublime Text 默
- 在实现贪吃蛇游戏时,首先需要使用python内置的turtle模块来实现创建游戏窗体与键盘按键的监听工作,然后使用freegames模块进行
- 技术背景在一些对python开源库代码的安全扫描中,我们有可能需要分析库中所使用到的函数是否会对代码的执行环境造成一些非预期的影响。典型的例
- 语法1.普通[expression for target in iterable]2.带条件[expression for target i
- 最近,接手的项目里,提供的数据文件格式简直让人看不下去,使用pandas打不开
- 函数的必选参数,指的是函数调用的时候必须传入的参数import mathdef cal (n): return n * nvar
- 模板引擎说明:模板文件就是按照一定的规则书写的展示效果的HTML文件 模板引擎就是负责按照指定规则进行替换的工具模板引擎选择jinja2一、
- 1. 首先介绍pip常用命令pip安装命令: pip install package_name pip升级命令:pip install –u
- 首先是准备工作Python 2.7.11:下载pythonPycharm:下载Pycharm其中python2和python3目前同步发行,
- 目录环境介绍原理介绍环境介绍Python 3.6 + OpenCV 3.4.1.15原理介绍首先,提取出模板中每一个数字的轮廓,再对信用卡图
- python爬虫模块PyQuery简介PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,
- 实例如下:from win32com.client import Dispatch import win32com.client
- 目录1.根据默认的行列索引操作1.1行删除1.2列删除2.根据自定义的行列索引操作2.1行删除2.2列删除本文介绍Pandas中DataFr
- YOLOv5的Backbone设计在上一篇文章《YOLOV5的anchor设定》中我们讨论了anchor的产生原理和检测过程,对YOLOv5
- asp 中处理文件上传以及删除时常用的自定义函数:删除文件,建立目录的程序,根据原文件名生成新的随机文件名,CMS替换函数,将所有开始,结束