mySQL UNION运算符的默认规则研究

 更新时间:2009年07月17日 00:38:32   作者:  
SQL UNION运算符的默认规则研究,学习union的朋友可以参考下。
复制代码 代码如下:

/* 建立数据表 */
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;

相关文章

  • 如何使用MySQL查询一年中每月的记录数

    如何使用MySQL查询一年中每月的记录数

    这篇文章主要给大家介绍了关于如何使用MySQL查询一年中每月的记录数的相关资料,文中通过实例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-09-09
  • 远程登录MySQL服务(小白入门篇)

    远程登录MySQL服务(小白入门篇)

    这篇文章主要为大家介绍了远程登录MySQL服务(小白入门篇)
    2023-05-05
  • 关于mysql数据库连接编码问题

    关于mysql数据库连接编码问题

    这篇文章主要介绍了关于mysql数据库连接编码问题,默认的编码和数据库表中的数据使用的编码是不一致的,如果是中文,那么在数据库中执行时已经是乱码了,需要的朋友可以参考下
    2023-04-04
  • 揭开SQL中NULL的神秘面纱

    揭开SQL中NULL的神秘面纱

    表的字段默认允许存放NULL值,这意味着,您在插入记录或者更新记录时,可以不为该字段指定值,此时该字段将存储NULL值,这篇文章将揭开SQL中NULL的神秘面纱。这个问题可能困扰着很多初级开发者
    2023-01-01
  • 一篇文章带你入门SQL编程

    一篇文章带你入门SQL编程

    这篇文章主要为大家详细介绍了SQL编程的入门方法,使用数据库,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • 一文搞懂什么是MySQL前缀索引

    一文搞懂什么是MySQL前缀索引

    所谓前缀索引,说白了就是对文本的前几个字符建立索引,有点类似于 Oracle 中对字段使用 Left 函数来建立函数索引,只不过 MySQL 的这个前缀索引在查询时是内部自动完成匹配的。本文将通过示例详细聊聊前缀索引,需要的可以参考一下
    2022-09-09
  • MySQL慢查日志的开启方式与存储格式详析

    MySQL慢查日志的开启方式与存储格式详析

    这篇文章主要给大家介绍了关于MySQL慢查日志的开启方式与存储格式的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用MySQL具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • MySQL数据被误删的解决方法

    MySQL数据被误删的解决方法

    之前被要求开发一个OA项目,需求还要及时生效(一边开发一边使用),有一次生产环境的一个bug本地没办法复现,在调试的过程中,我俩当作开发环境很自然的把数据给删了,所以在这里记录一下MySQL数据备份和恢复的方法及操作,希望可以帮助到跟我一样的小伙伴
    2024-01-01
  • 美团DB数据同步到数据仓库的架构与实践

    美团DB数据同步到数据仓库的架构与实践

    今天小编就为大家分享一篇关于美团DB数据同步到数据仓库的架构与实践,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • winxp 安装MYSQL 出现Error 1045 access denied 的解决方法

    winxp 安装MYSQL 出现Error 1045 access denied 的解决方法

    自己遇到了这个问题,也找了很久才解决,就整理一下,希望对大家有帮助!
    2010-07-07

最新评论