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;

标签:SQL,UNION运算符
0
投稿

猜你喜欢

  • Python 实现OpenCV格式和PIL.Image格式互转

    2021-08-03 03:41:42
  • BootStrap的alert提示框的关闭后再显示怎么解决

    2024-04-28 09:50:24
  • Python编程应用设计原则详解

    2021-04-08 20:13:05
  • 一个不错的js+css二级分类菜单代码

    2007-12-28 21:22:00
  • python通过邮件服务器端口发送邮件的方法

    2021-10-18 02:34:09
  • SEO与“nofollow”及“external nofollow”

    2007-12-15 09:31:00
  • python Copula 实现绘制散点模型

    2023-07-24 14:02:37
  • Django 生成登陆验证码代码分享

    2021-07-31 06:48:21
  • Python绘制堆叠柱状图的实例

    2022-01-04 06:14:55
  • Pytest框架之fixture详解(三)

    2023-06-20 12:05:27
  • Python自动化实战之接口请求的实现

    2021-01-15 15:44:58
  • MySQL中索引优化distinct语句及distinct的多字段操作

    2024-01-18 20:43:38
  • MySQL 复制表详解及实例代码

    2024-01-22 22:49:16
  • 详解python 3.6 安装json 模块(simplejson)

    2023-08-04 10:55:03
  • javascript获取select标签选中的值

    2024-05-09 10:34:41
  • Python语法学习之进程的创建与常用方法详解

    2023-11-08 11:23:34
  • JS图片根据鼠标滚动延时加载的实例代码

    2024-04-22 13:22:33
  • Vue图片裁剪功能实现代码

    2024-05-29 22:50:11
  • Pandas替换及部分替换(replace)实现流程详解

    2023-11-04 02:58:38
  • python docx的超链接网址和链接文本操作

    2021-06-05 22:10:55
  • asp之家 网络编程 m.aspxhome.com