MySQL UNION运算符的默认规则研究
2022-11-12 09:20:41
内容摘要
这篇文章主要为大家详细介绍了MySQL UNION运算符的默认规则研究,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
/* 建立数
文章正文
这篇文章主要为大家详细介绍了MySQL UNION运算符的默认规则研究,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <code> /* 建立数据表 */ 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本身有剔除重复项的作用 */ /* 查询手册定义 */ /* </code> |
注:关于MySQL UNION运算符的默认规则研究的内容就先介绍到这里,更多相关文章的可以留意
代码注释