<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Toán tử UNION ALL trong SQL Server</title>
<meta name="description" content="Toán tử UNION ALL trong SQL Server - Savefile - Tin Tức -...">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Toán tử UNION ALL trong SQL Server">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;toan-tu-union-all-trong-sql-server-8911.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/toan-tu-union-all-trong-sql-server-8911.html">
<link rel="shortcut icon" href="https://nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/toan-tu-union-all-trong-sql-server-8911.html">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://nguoicodonvn2008.info/">https://nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Toán tử UNION ALL trong SQL Server</h1>
		<ul class="list-inline">
			<li>Chủ nhật - 25/08/2024 23:48</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Toán tử UNION ALL trong SQL Server" src="https://st.quantrimang.com/photos/image/2023/09/29/union-vs-union-all5.png" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;"><strong>UNION ALL trong SQL Server</strong>&nbsp;là gì?&nbsp;<strong>Cách dùng UNION ALL SQL Server</strong>&nbsp;như thế nào? Hãy cùng Quantrimang.com tìm hiểu nhé!</p>

<p style="text-align: justify;">SQL Server có rất nhiều kiến thức thú vị cho bạn tìm hiểu. Bên cạnh các hàm thông dụng, toán tử là nội dung bạn nên tìm hiểu khi sử dụng SQL Server.</p>

<p style="text-align: justify;">UNION và UNION ALL là hai toán tử SQL cơ bản nhất được dùng trong database để kết hợp nhóm kết quả từ nhiều bảng. Khác với toán tử&nbsp;<a href="https://quantrimang.com/hoc/toan-tu-union-trong-sql-server-148035" title="Toán tử UNION trong SQL Server ">UNION</a>, toán tử UNION ALL trả về tất cả các hàng từ truy vấn và không xóa các hàng trùng lặp.</p>

<p style="text-align: justify;">Mỗi lệnh SELECT trong toán tử UNION phải có cùng số cột trong bộ kết quả với kiểu dữ liệu tương ứng.</p>

<h3 style="text-align: justify;">Cú pháp toán tử UNION ALL</h3>

<pre id="pre0" style="text-align: justify;">
SELECT bieu_thuc1, bieu_thuc2, … bieu_thucnFROM bang&#91;WHERE dieu_kien&#93;UNION ALLSELECT bieu_thuc1, bieu_thuc2, … bieu_thucnFROM bang&#91;WHERE dieu_kien&#93;;</pre>

<h3 style="text-align: justify;">Tên biến hoặc giá trị biến</h3>

<p style="text-align: justify;"><strong>bieu_thuc1, bieu_thuc2, … bieu_thucn</strong></p>

<p style="text-align: justify;">Cột hoặc giá trị tính toán muốn lấy.</p>

<p style="text-align: justify;"><strong>bang</strong></p>

<p style="text-align: justify;">Bảng muốn lấy bản ghi. Phải có ít nhất 1 bảng trong mệnh đề&nbsp;FROM.</p>

<p style="text-align: justify;"><strong>WHERE dieu_kien</strong></p>

<p style="text-align: justify;">Tùy chọn. Điều kiện phải đáp ứng để bản ghi được chọn.</p>

<p style="text-align: justify;"><strong>Lưu ý:</strong></p>

<ul>
	<li style="text-align: justify;">2 lệnh SELECT phải có cùng số biểu thức</li>
	<li style="text-align: justify;">Số cột tương ứng trong mỗi lệnh SELECT phải có cùng kiểu dữ liệu</li>
	<li style="text-align: justify;">Toán tử UNION không xóa các hàng trùng nhau.</li>
</ul>

<p style="text-align: justify;"><strong>Ví dụ - trả về một trường thông tin</strong></p>

<pre id="pre1" style="text-align: justify;">
SELECT sanpham_idFROM sanphamUNION ALLSELECT sanpham_idFROM hangtonkho;</pre>

<p style="text-align: justify;">Ví dụ này trả về nhiều sanpham_id trong bộ kết quả nếu chúng có xuất hiện trong cả bảng sanpham và hangtonkho. Nếu muốn xóa các bản trùng lặp, hãy dùng toàn tử UNION.</p>

<p style="text-align: justify;"><strong>Ví dụ - dùng ORDER BY</strong></p>

<p style="text-align: justify;">Toán tử UNION ALL có thể dùng mệnh đề&nbsp;ORDER BY&nbsp;để sắp xếp kết quả truy vấn.</p>

<div style="text-align: justify;">&nbsp;</div>

<pre id="pre2" style="text-align: justify;">
SELECT danhba_id, danhba_tenFROM danhbaWHERE ten_trang = ‘QuanTriMang.com’UNION ALLSELECT congty_id, congty_tenFROM congtyWHERE ten_trang = ‘TrangCuaBan.com’ORDER BY 2;</pre>

<p style="text-align: justify;">Ở ví dụ này, vì tên cột ở 2 lệnh SELECT khác nhau nên sẽ dễ hơn khi tham chiếu tới cột trong lệnh ORDER BY bằng vị trí trong bộ kết quả. Trong ví dụ trên, ta lọc kết quả theo danhba_ten / congty_ten theo thứ tự tăng dần, như đã nói bằng cụm từ ORDER BY 2.</p>

<p style="text-align: justify;">danhba_ten / congty_ten nằm ở vị trí thứ 2 trong bộ kết quả.</p>

<h2 style="text-align: justify;">Sự khác biệt giữa Union và Union All in SQL Server</h2>

<table cellpadding="3" cellspacing="3">
	<tbody>
		<tr>
			<td>
			<p style="text-align: justify;"><strong>UNION</strong></p>
			</td>
			<td>
			<p style="text-align: justify;"><strong>UNION ALL</strong></p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">UNION kết hợp nhóm kết quả từ nhiều bảng và trả về các bản ghi riêng biệt vào một nhóm kết quả duy nhất.</p>
			</td>
			<td>
			<p style="text-align: justify;">UNION ALL kết hợp nhóm kết quả từ nhiều bảng và trả về toàn bộ bản ghi vào một nhóm kết quả duy nhất.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Cú pháp cơ bản của toán tử UNION:</p>

			<pre id="pre3">
<code>SELECT column_list FROM table1
UNION
SELECT column_list FROM table2;</code></pre>
			</td>
			<td>
			<p style="text-align: justify;">Cú pháp cơ bản của toán tử UNION ALL:</p>

			<pre id="pre4">
<code>SELECT column_list FROM table1
UNION ALL
SELECT column_list FROM table2;</code></pre>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Nó có tính năng mặc định loại bỏ các hàng trùng lặp từ đầu ra.</p>
			</td>
			<td>
			<p style="text-align: justify;">Không có tính năng mặc định loại bỏ các hàng trùng lặp.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Hiệu suất chậm vì nó cần thời gian để tìm, rồi loại bỏ các bản ghi trùng lặp.</p>
			</td>
			<td>
			<p style="text-align: justify;">Hiệu suất nhanh vì không xóa các hàng trùng lặp.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Hầu hết người dùng database đều thích sử dụng toán tử này.</p>
			</td>
			<td>
			<p style="text-align: justify;">Hầu hết người dùng database không thích dùng toán tử này.</p>
			</td>
		</tr>
	</tbody>
</table>

<p style="text-align: justify;">Để hiểu rõ hơn về sự khác biệt giữa Union và Union All SQL Server, hãy cùng nhau xem xét ví dụ sau:</p>

<p style="text-align: justify;">Giả sử bạn có bảng tên&nbsp;<strong>Student&nbsp;</strong>và&nbsp;<strong>Student2</strong>, chứa dữ liệu sau:</p>

<p style="text-align: justify;"><strong>Bảng: Student</strong></p>

<p style="text-align: justify;"><strong><img alt="Bảng học sinh" data-i="0" data-src="https://st.quantrimang.com/photos/image/2023/09/29/union-vs-union-all3.png" data-was-processed="true" height="107" src="https://st.quantrimang.com/photos/image/2023/09/29/union-vs-union-all3.png" width="317" /></strong></p>

<p style="text-align: justify;"><strong>Bảng: Student2</strong></p>

<p style="text-align: justify;"><strong><img alt="Bảng học sinh 2" data-i="1" data-src="https://st.quantrimang.com/photos/image/2023/09/29/union-vs-union-all4.png" data-was-processed="true" height="107" src="https://st.quantrimang.com/photos/image/2023/09/29/union-vs-union-all4.png" width="322" /></strong></p>

<p style="text-align: justify;">Lệnh SQL sau trả về tên riêng của các thành phố từ cả hai bảng bằng truy vấn UNION:</p>

<div style="text-align: justify;"><iframe allow="autoplay" allowtransparency="true" aria-label="Advertisement" data-google-container-id="a!3" data-google-query-id="CLCB8u7fkYgDFUXMTAIdEVc3IA" data-load-complete="true" frameborder="0" height="0" hspace="0" id="aswift_2" marginheight="0" marginwidth="0" name="aswift_2" sandbox="allow-scripts allow-same-origin allow-forms allow-presentation allow-popups" scrolling="no" tabindex="0" title="Advertisement" vspace="0" width="694"></iframe></div>

<pre id="pre5" style="text-align: justify;">
SELECT City FROM student  
UNION  
SELECT City FROM student2  
ORDER BY City;  </pre>

<p style="text-align: justify;">Sau khi chạy lệnh trên, bạn sẽ nhận được kết quả bên dưới vì toán tử Union chỉ trả về giá trị riêng biệt.</p>

<p style="text-align: justify;"><img alt="Kết quả cho thấy sự khác biệt giữa union và union all" data-i="2" data-src="https://st.quantrimang.com/photos/image/2023/09/29/union-vs-union-all5.png" data-was-processed="true" height="268" src="https://st.quantrimang.com/photos/image/2023/09/29/union-vs-union-all5.png" width="597" /></p>

<p style="text-align: justify;">Lệnh SQL sau trả về tất cả tên thành phố bao gồm các những giá trị trùng lặp từ cả hai bảng bằng truy vấn UNION ALL:</p>

<pre id="pre6" style="text-align: justify;">
SELECT City FROM student  
UNION ALL  
SELECT City FROM student2  
ORDER BY City;  </pre>

<p style="text-align: justify;">Sau khi chạy lệnh trên, bạn sẽ nhận được kết quả dưới đây vì toán tử Union All trả về toàn bộ bản ghi mà không cần phải loại bỏ các giá trị riêng biệt.</p>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com:
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/toan-tu-union-all-trong-sql-server-8911.html" title="Toán tử UNION ALL trong SQL Server">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/toan-tu-union-all-trong-sql-server-8911.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://nguoicodonvn2008.info",
            "logo": "https://nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>