跳至正文
-
收藏很文博客的最新网址 www.hw97.com,不错过任何精彩内容。
很文博客 很文博客 很文博客

生活点滴 贵在分享

很文博客 很文博客 很文博客

生活点滴 贵在分享

  • 首页
  • 站务联系
  • 首页
  • 站务联系
关

搜索

数据库

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

2018年6月17日
0

SQL UNION运算符的默认规则研究,学习union的朋友可以参考下。

  1. /* 建立数据表 */
  2. 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;
  3. 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;
  4. /* 插入模拟记录 */
  5. insert into td_base_data(userId) values(1);
  6. insert into td_base_data(userId) values(45);
  7. insert into td_base_data(userId) values(45);
  8. insert into td_base_data(userId) values(1);
  9. insert into td_base_data(userId) values(45);
  10. insert into td_base_data_20090527(userId) values(1);
  11. insert into td_base_data_20090527(userId) values(45);
  12. insert into td_base_data_20090527(userId) values(45);
  13. insert into td_base_data_20090527(userId) values(1);
  14. insert into td_base_data_20090527(userId) values(45);
  15. insert into td_base_data_20090527(userId) values(45);
  16. /* 查询测试 */
  17. select count(userId) as cnumber from td_base_data where userId = '45';
  18. /* 3 */
  19. select count(userId) as cnumber from td_base_data_20090527 where userId = '45';
  20. /* 4 */
  21. select (select count(userId) from td_base_data where userId = '45') + (select count(userId) from td_base_data_20090527 where userId = '45') as cnumber;
  22. /* 7 */
  23. select count(*) from
  24. (
  25. select id from td_base_data where userId = '45'
  26. union
  27. select id from td_base_data_20090527 where userId = '45'
  28. ) as tx;
  29. /* 4 */
  30. select count(*) from
  31. (
  32. select * from td_base_data where userId = '45'
  33. union
  34. select * from td_base_data_20090527 where userId = '45'
  35. ) as tx;
  36. /* 4 */
  37. /* 证明在mysql中,union本身有剔除重复项的作用 */
  38. /* 查询手册定义 */
  39. /*

查询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
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;

标签:

MySQLUNION运算符
作者

很文博客

关注我
其他文章
上一个

mysql 常用数据库语句

下一个

MYSQL ERROR 1045 (28000): Access denied for user (using password: YES)问题的解决

暂无评论!成为第一个。

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

近期浏览最多文章

  • 作品观看平台+科学上网工具(不定期更新……) - 5,816 浏览
  • 「博多彩叶」出道作品番号及封面,「博多彩叶」个人简介 - 2,261 浏览
  • 「河北彩伽」最新作品SNOS-056介绍及封面预览 - 2,086 浏览
  • 森日向子(Mori-Hinako)最新作品MIAB-021介绍及封面预览 - 1,928 浏览
  • 「河北彩伽」最新作品SNOS-003介绍及封面预览 - 1,779 浏览

热门标签

Julia miru MySQL 三上悠亚 三上悠亜 习惯 伊藤舞雪 传奇故事 动物故事 励志名言 励志故事 危机故事 哲学哲理 哲理小语 哲理故事 寓言故事 小花のん 小花暖 希腊寓言故事 帝王故事 心情说说 惊险故事 战争故事 明里つむぎ 明里䌷 松本いちか 枫富爱 桃乃木かな 桃乃木香奈 桜空もも 梦乃あいか 樱空桃 欢迎词 河北彩花 波多野结衣 生日祝福语 生活小语 祝福短信 祝福语 神宫寺ナオ 神宫寺奈绪 童话故事 美谷朱里 自然人文 藤森里穂
Copyright 2026 — 很文博客. All rights reserved. Sitemap |站务联系 |备用网址