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;


猜你喜欢
- httpparser介绍:1.解析字节类型的http与https请求数据:2.支持已k-v形式修改请求数据:3.支持重新编码请求数据源码im
- 本文实例讲述了JS弹出窗口插件zDialog简单用法。分享给大家供大家参考,具体如下:因为没有元素可以显示到Frameset上面去,所以重新
- 工作中我们经常要两段代码的区别,或者需要查看接口返回的字段与预期是否一致,如何快速定位出两者的差异?除了一些对比的工具比如Beyond Co
- 最近发现自己的博客打开很慢,通过ie浏览器打开速度还可以,使用任何第三方浏览器打开都超级慢,以为是HTML代码元素导致,一番比对后没有发现不
- 形式pandas.Series(data=None, index=None, dtype=None,&nbs
- 体系结构 Microsoft按照客户/服务器体系结构的分布进行操作。这种方法产生不必要的代价和复杂性。在Internet中,Oracle已经
- 本文实例讲述了django框架CSRF防护。分享给大家供大家参考,具体如下:CSRF防护一、什么是CSRF?CSRF: Cross-site
- 读取数据(Reading data)TensorFlow输入数据的方式有四种:tf.data API:可以很容易的构建一个复杂的输入通道(p
- 前言数据分析就是将数据以各种图表的形式展现给领导,供领导做决策用,因此熟练掌握饼图、柱状图、线图等图表制作是一个数据分析师必备的技能。Pyt
- 误区 #29:可以通过对堆建聚集索引再DROP后进行堆上的碎片整理Nooooooooooooo!!! &
- Display SQL Server Login Mode. Supported Plat
- 本文实例总结了python在windows和linux下获得本机本地ip地址方法。分享给大家供大家参考。具体分析如下:python的sock
- jQuery之所以如此流行并被从大公司到个人博客的几乎每个人都广泛使用,是因为它上手和使用相当简单,而且为我们提供了一些人都不知道的相当棒的
- ---- 一、 引言: ---- 回滚段是数据库的一部分,它记录数据库变更的信息。使用这些信息实现数据库的读一致性及其恢复。若回滚段出现故障
- 用户登录验证脚本,Chkpwd.asp<% '=======用户登录验证脚本======= '
- 1、创建项目打开pycharm 终端,输入如下,创建项目# 进入pycharm 项目目录下cd pyWebdjango-admin star
- Python字符串问题在arcpy中版本为 python2.x在QGIS中版本为 python2.x 或者 python3.xpython2
- 以前看到 andy的关于“Quiet Structure”觉的很不错,于是今天到她的个人站点上逛逛,发现不少好的文章,今天介绍的是
- 你们要的3D太阳系图片上传之后不知为何帧率降低了许多。。。日地月三体所谓三体,就是三个物体在重力作用下的运动。由于三点共面,所以三个质点仅在
- #写在前面,这个程序我已经弄出来了,但是因为黄牛泛滥以及懒人太多,整个程序的代码就不贴出来了,这里纯粹就是技术交流。只做技术交流、、、、、