《MYSQL數(shù)據(jù)庫mySQL UNION運算符的默認(rèn)規(guī)則研究》要點:
本文介紹了MYSQL數(shù)據(jù)庫mySQL UNION運算符的默認(rèn)規(guī)則研究,希望對您有用。如果有疑問,可以聯(lián)系我們。
代碼如下:
/* 建立數(shù)據(jù)表 */
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本身有剔除重復(fù)項的作用 */
/* 查詢手冊定義 */
/*
查詢mysql參考手冊:
13.2.7.2. UNION語法
如果您對UNION不使用關(guān)鍵詞ALL,則所有返回的行都是唯一的,如同您已經(jīng)對整個結(jié)果集合使用了DISTINCT.如果您指定了ALL,您會從所有用過的SELECT語句中得到所有匹配的行.
DISTINCT關(guān)鍵詞是一個自選詞,不起任何作用,但是根據(jù)SQL標(biāo)準(zhǔn)的要求,在語法中允許采用.(在MySQL中,DISTINCT代表一個共用體的默認(rèn)工作性質(zhì).)
*/
/* 證明在mysql中,union默認(rèn)就是DISTINCT的 */
/*
查詢mssql參考手冊:
Transact-SQL 參考
UNION 運算符:
使用 UNION 組合兩個查詢的結(jié)果集的兩個基本規(guī)則是:
1.所有查詢中的列數(shù)和列的順序必須相同.
2.數(shù)據(jù)類型必須兼容.
參數(shù):
UNION
指定組合多個結(jié)果集并將其作為單個結(jié)果集返回.
ALL
在結(jié)果中包括所有的行,包括重復(fù)行.如果沒有指定,則刪除重復(fù)行.
*/
/* 證明在mssql中,union默認(rèn)也是DISTINCT的 */
/* 查詢標(biāo)準(zhǔn)定義 */
/*
查詢SQL2003標(biāo)準(zhǔn):
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標(biāo)準(zhǔn)定義了DISTINCT就是union的默認(rèn)值 */
/* 正確查詢,同時應(yīng)該在兩表的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數(shù)據(jù)庫mySQL UNION運算符的默認(rèn)規(guī)則研究》是否對您有啟發(fā),歡迎查看更多與《MYSQL數(shù)據(jù)庫mySQL UNION運算符的默認(rèn)規(guī)則研究》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/14144.html