您好,登錄后才能下訂單哦!
下文給大家?guī)?lái)有關(guān)MySQL中將多個(gè)select語(yǔ)句的查詢(xún)結(jié)果合并一起的方法內(nèi)容,相信大家一定看過(guò)類(lèi)似的文章。我們給大家?guī)?lái)的有何不同呢?一起來(lái)看看正文部分吧,相信看完MySQL中將多個(gè)select語(yǔ)句的查詢(xún)結(jié)果合并一起的方法你一定會(huì)有所收獲。
1. 背景
* 全并查詢(xún)結(jié)果是將多個(gè) select 語(yǔ)句的查詢(xún)結(jié)果合并到一起。
* 參與合并的結(jié)果集需要字段統(tǒng)一。
* 字段可以用空字符串''代替。
2. 合并查詢(xún)結(jié)果實(shí)戰(zhàn) [ users1 and users2 ]
* 查看 users1 表和 users2 表結(jié)構(gòu)
mysql> desc users1; +-------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | name | varchar(64) | NO | | NULL | | | sex | enum('M','F') | NO | | NULL | | | age | int(11) | NO | | NULL | | +-------+---------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> desc users2; +-------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | name | varchar(64) | NO | | NULL | | | sex | enum('M','F') | NO | | NULL | | | age | int(11) | NO | | NULL | | +-------+---------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)
* 查看 users1 表和 users2 表數(shù)據(jù)
users1和users2表中有相同字段 tom
mysql> select * from users1; +----+------+-----+-----+ | id | name | sex | age | +----+------+-----+-----+ | 1 | tom | M | 25 | | 2 | jak | F | 42 | +----+------+-----+-----+ 2 rows in set (0.00 sec) mysql> select * from users2; +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 25 | | 2 | lisea | M | 42 | +----+-------+-----+-----+ 2 rows in set (0.00 sec)
* union 合并并去重
mysql> (select * from users1) union (select * from users2); +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 25 | | 2 | jak | F | 42 | | 2 | lisea | M | 42 | +----+-------+-----+-----+ 3 rows in set (0.00 sec)
* union all 只全并不去重
mysql> (select * from users1) union all (select * from users2); +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 25 | | 2 | jak | F | 42 | | 1 | tom | M | 25 | | 2 | lisea | M | 42 | +----+-------+-----+-----+ 4 rows in set (0.01 sec)
* 查看union 性能分析
[ 使用了臨時(shí)表 ]
mysql> explain (select * from users1) union (select * from users2); +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | 1 | PRIMARY | users1 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL | | 2 | UNION | users2 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL | | NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ 3 rows in set, 1 warning (0.01 sec)
* 查看union all 性能分析
[ 未使用臨時(shí)表 ]
mysql> explain (select * from users1) union all (select * from users2); +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+ | 1 | PRIMARY | users1 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL | | 2 | UNION | users2 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL | +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+ 2 rows in set, 1 warning (0.01 sec)
3. union 與 union all 總結(jié)
* union 相對(duì)于 union all多了一步去重操作,此操作會(huì)創(chuàng)建臨時(shí)表,降低性能。
* 當(dāng)兩邊結(jié)果集數(shù)據(jù)相對(duì)都確定了唯一性,推薦使用union all。
4. 總結(jié)
以需求驅(qū)動(dòng)技術(shù),技術(shù)本身沒(méi)有優(yōu)略之分,只有業(yè)務(wù)之分。
對(duì)于上文關(guān)于MySQL中將多個(gè)select語(yǔ)句的查詢(xún)結(jié)果合并一起的方法,大家覺(jué)得是自己想要的嗎?如果想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。