最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mySQL UNION運(yùn)算符的默認(rèn)規(guī)則研究

 更新時(shí)間:2009年07月17日 00:38:32   作者:  
SQL UNION運(yùn)算符的默認(rèn)規(guī)則研究,學(xué)習(xí)union的朋友可以參考下。
復(fù)制代碼 代碼如下:

/* 建立數(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);
/* 查詢測(cè)試 */
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ù)項(xiàng)的作用 */

/* 查詢手冊(cè)定義 */
/*

查詢mysql參考手冊(cè):
13.2.7.2. UNION語(yǔ)法
如果您對(duì)UNION不使用關(guān)鍵詞ALL,則所有返回的行都是唯一的,如同您已經(jīng)對(duì)整個(gè)結(jié)果集合使用了DISTINCT。如果您指定了ALL,您會(huì)從所有用過(guò)的SELECT語(yǔ)句中得到所有匹配的行。
DISTINCT關(guān)鍵詞是一個(gè)自選詞,不起任何作用,但是根據(jù)SQL標(biāo)準(zhǔn)的要求,在語(yǔ)法中允許采用。(在MySQL中,DISTINCT代表一個(gè)共用體的默認(rèn)工作性質(zhì)。)
*/
/* 證明在mysql中,union默認(rèn)就是DISTINCT的 */
/*
查詢mssql參考手冊(cè):
Transact-SQL 參考
UNION 運(yùn)算符:
使用 UNION 組合兩個(gè)查詢的結(jié)果集的兩個(gè)基本規(guī)則是:
1.所有查詢中的列數(shù)和列的順序必須相同。
2.數(shù)據(jù)類型必須兼容。
參數(shù):
UNION
指定組合多個(gè)結(jié)果集并將其作為單個(gè)結(jié)果集返回。
ALL
在結(jié)果中包含所有的行,包括重復(fù)行。如果沒(méi)有指定,則刪除重復(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.
*/
/* 可見(jiàn)SQL2003標(biāo)準(zhǔn)定義了DISTINCT就是union的默認(rèn)值 */

/* 正確查詢,同時(shí)應(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;

相關(guān)文章

  • MySQL安裝及初始密碼設(shè)置方式

    MySQL安裝及初始密碼設(shè)置方式

    這篇文章主要介紹了MySQL安裝及初始密碼設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • mysql慢查詢優(yōu)化之從理論和實(shí)踐說(shuō)明limit的優(yōu)點(diǎn)

    mysql慢查詢優(yōu)化之從理論和實(shí)踐說(shuō)明limit的優(yōu)點(diǎn)

    今天小編就為大家分享一篇關(guān)于mysql慢查詢優(yōu)化之從理論和實(shí)踐說(shuō)明limit的優(yōu)點(diǎn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • Mysql 設(shè)置boolean類型的操作

    Mysql 設(shè)置boolean類型的操作

    這篇文章主要介紹了Mysql 設(shè)置boolean類型的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • MySQL子查詢?cè)敿?xì)教程

    MySQL子查詢?cè)敿?xì)教程

    這篇文章主要介紹了MySQL子查詢?cè)敿?xì)教程的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Linux下安裝mysql-8.0.20的教程詳解

    Linux下安裝mysql-8.0.20的教程詳解

    這篇文章主要介紹了Linux下安裝mysql8.0.20的教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • MySQL使用觸發(fā)器實(shí)現(xiàn)數(shù)據(jù)自動(dòng)更新的應(yīng)用實(shí)例

    MySQL使用觸發(fā)器實(shí)現(xiàn)數(shù)據(jù)自動(dòng)更新的應(yīng)用實(shí)例

    觸發(fā)器是非常常見(jiàn)的自動(dòng)化數(shù)據(jù)庫(kù)操作方式,無(wú)論是在數(shù)據(jù)更新、刪除還是需要自動(dòng)添加一些內(nèi)容到數(shù)據(jù)表上,觸發(fā)器都可以發(fā)揮作用,熟悉 SQL 的基本語(yǔ)法和一些常見(jiàn)的用例,可以幫助你合理地設(shè)置自己的數(shù)據(jù)庫(kù)操作流程,
    2024-01-01
  • MySQL安裝后不能用是什么情況該如何解決

    MySQL安裝后不能用是什么情況該如何解決

    之前安裝過(guò)MYSQL好像不用手動(dòng)啟動(dòng)服務(wù),具體也忘記了,但我上回給公司安裝的那個(gè)是要手動(dòng)安裝服務(wù)的,如果mysql剛剛安裝不能用,可能是服務(wù)沒(méi)有安裝
    2014-03-03
  • 關(guān)于Mysql搭建主從復(fù)制功能的步驟實(shí)現(xiàn)

    關(guān)于Mysql搭建主從復(fù)制功能的步驟實(shí)現(xiàn)

    這篇文章主要介紹了關(guān)于Mysql搭建主從復(fù)制功能的步驟實(shí)現(xiàn),在實(shí)際的生產(chǎn)中,為了解決Mysql的單點(diǎn)故障已經(jīng)提高M(jìn)ySQL的整體服務(wù)性能,一般都會(huì)采用主從復(fù)制,需要的朋友可以參考下
    2023-05-05
  • mysql數(shù)據(jù)庫(kù)的分區(qū)表示例代碼

    mysql數(shù)據(jù)庫(kù)的分區(qū)表示例代碼

    這篇文章主要介紹了mysql數(shù)據(jù)庫(kù)的分區(qū)表的相關(guān)資料,文章介紹了兩種創(chuàng)建SQL表分區(qū)的方法,分別是手動(dòng)創(chuàng)建和使用MySQL的定時(shí)事件來(lái)自動(dòng)創(chuàng)建分區(qū),手動(dòng)創(chuàng)建分區(qū)時(shí),需要在代碼中判斷分區(qū)并新增,可能會(huì)引入一些問(wèn)題,需要的朋友可以參考下
    2024-11-11
  • mysql使用物理備份安裝xtrabackup的詳細(xì)過(guò)程

    mysql使用物理備份安裝xtrabackup的詳細(xì)過(guò)程

    這篇文章主要介紹了mysql使用物理備份安裝xtrabackup的詳細(xì)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-05-05

最新評(píng)論

许昌市| 泉州市| 万源市| 白沙| 和林格尔县| 土默特左旗| 长阳| 庄河市| 华安县| 乌恰县| 安顺市| 固安县| 文安县| 明溪县| 庆安县| 高雄市| 兴海县| 思茅市| 浪卡子县| 六枝特区| 兖州市| 荆门市| 虎林市| 紫阳县| 大新县| 扬中市| 墨玉县| 西昌市| 黔南| 射洪县| 宝坻区| 武隆县| 肇东市| 大埔县| 卢龙县| 临安市| 邛崃市| 吉隆县| 宝应县| 德庆县| 夏邑县|