国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mySQL UNION運算符的默認規則研究

2020-01-19 00:19:21
字體:
來源:轉載
供稿:網友
復制代碼 代碼如下:

/* 建立數據表 */
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;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台山市| 铅山县| 剑川县| 富裕县| 丰顺县| 东兰县| 株洲市| 屯留县| 夹江县| 清河县| 石棉县| 邛崃市| 八宿县| 岚皋县| 彰化县| 那坡县| 盐城市| 伊春市| 朝阳市| 吐鲁番市| 石首市| 铜山县| 佛坪县| 施秉县| 双桥区| 湟中县| 长春市| 侯马市| 胶州市| 竹山县| 江口县| 渑池县| 孝昌县| 石狮市| 旅游| 宜宾市| 丹凤县| 华安县| 凤山县| 浪卡子县| 安新县|