亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mySQL UNION運算符的默認規(guī)則研究

 更新時間:2009年07月17日 00:38:32   作者:  
SQL UNION運算符的默認規(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);
/* 查詢測試 */
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標準的要求,在語法中允許采用。(在MySQL中,DISTINCT代表一個共用體的默認工作性質(zhì)。)
*/
/* 證明在mysql中,union默認就是DISTINCT的 */
/*
查詢mssql參考手冊:
Transact-SQL 參考
UNION 運算符:
使用 UNION 組合兩個查詢的結(jié)果集的兩個基本規(guī)則是:
1.所有查詢中的列數(shù)和列的順序必須相同。
2.數(shù)據(jù)類型必須兼容。
參數(shù):
UNION
指定組合多個結(jié)果集并將其作為單個結(jié)果集返回。
ALL
在結(jié)果中包含所有的行,包括重復(fù)行。如果沒有指定,則刪除重復(fù)行。
*/
/* 證明在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的默認值 */

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

    這篇文章主要給大家介紹了關(guān)于如何使用MySQL查詢一年中每月的記錄數(shù)的相關(guān)資料,文中通過實例代碼以及圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-09-09
  • 遠程登錄MySQL服務(wù)(小白入門篇)

    遠程登錄MySQL服務(wù)(小白入門篇)

    這篇文章主要為大家介紹了遠程登錄MySQL服務(wù)(小白入門篇)
    2023-05-05
  • 關(guān)于mysql數(shù)據(jù)庫連接編碼問題

    關(guān)于mysql數(shù)據(jù)庫連接編碼問題

    這篇文章主要介紹了關(guān)于mysql數(shù)據(jù)庫連接編碼問題,默認的編碼和數(shù)據(jù)庫表中的數(shù)據(jù)使用的編碼是不一致的,如果是中文,那么在數(shù)據(jù)庫中執(zhí)行時已經(jīng)是亂碼了,需要的朋友可以參考下
    2023-04-04
  • 揭開SQL中NULL的神秘面紗

    揭開SQL中NULL的神秘面紗

    表的字段默認允許存放NULL值,這意味著,您在插入記錄或者更新記錄時,可以不為該字段指定值,此時該字段將存儲NULL值,這篇文章將揭開SQL中NULL的神秘面紗。這個問題可能困擾著很多初級開發(fā)者
    2023-01-01
  • 一篇文章帶你入門SQL編程

    一篇文章帶你入門SQL編程

    這篇文章主要為大家詳細介紹了SQL編程的入門方法,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 一文搞懂什么是MySQL前綴索引

    一文搞懂什么是MySQL前綴索引

    所謂前綴索引,說白了就是對文本的前幾個字符建立索引,有點類似于?Oracle?中對字段使用?Left?函數(shù)來建立函數(shù)索引,只不過?MySQL?的這個前綴索引在查詢時是內(nèi)部自動完成匹配的。本文將通過示例詳細聊聊前綴索引,需要的可以參考一下
    2022-09-09
  • MySQL慢查日志的開啟方式與存儲格式詳析

    MySQL慢查日志的開啟方式與存儲格式詳析

    這篇文章主要給大家介紹了關(guān)于MySQL慢查日志的開啟方式與存儲格式的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • MySQL數(shù)據(jù)被誤刪的解決方法

    MySQL數(shù)據(jù)被誤刪的解決方法

    之前被要求開發(fā)一個OA項目,需求還要及時生效(一邊開發(fā)一邊使用),有一次生產(chǎn)環(huán)境的一個bug本地沒辦法復(fù)現(xiàn),在調(diào)試的過程中,我倆當(dāng)作開發(fā)環(huán)境很自然的把數(shù)據(jù)給刪了,所以在這里記錄一下MySQL數(shù)據(jù)備份和恢復(fù)的方法及操作,希望可以幫助到跟我一樣的小伙伴
    2024-01-01
  • 美團DB數(shù)據(jù)同步到數(shù)據(jù)倉庫的架構(gòu)與實踐

    美團DB數(shù)據(jù)同步到數(shù)據(jù)倉庫的架構(gòu)與實踐

    今天小編就為大家分享一篇關(guān)于美團DB數(shù)據(jù)同步到數(shù)據(jù)倉庫的架構(gòu)與實踐,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • winxp 安裝MYSQL 出現(xiàn)Error 1045 access denied 的解決方法

    winxp 安裝MYSQL 出現(xiàn)Error 1045 access denied 的解決方法

    自己遇到了這個問題,也找了很久才解決,就整理一下,希望對大家有幫助!
    2010-07-07

最新評論