<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Toán tử UNION trong SQL Server</title>
<meta name="description" content="Toán tử UNION trong SQL Server - 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-trong-sql-server-9196.html">
<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 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-trong-sql-server-9196.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-trong-sql-server-9196.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/toan-tu-union-trong-sql-server-9196.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.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://www.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://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.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://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.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://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Toán tử UNION trong SQL Server</h1>
		<ul class="list-inline">
			<li>Thứ tư - 16/10/2024 23:17</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 trong SQL Server" src="https://st.quantrimang.com/photos/image/2019/02/21/HAM-COUNT-200-size-80x80-znd.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;"><strong>Union trong SQL Server&nbsp;</strong>là gì?<strong>&nbsp;Cách dùng Union SQL Server&nbsp;</strong>như thế nào? Hãy cùng Quantrimang.com tìm hiểu nhé!</p>

<p style="text-align: justify;">Bạn sẽ không khó xin được việc làm tốt với mức lương cao nếu biết lập trình. Đây hiện đang là một trong số ngành được săn đón nhất hiện nay. Chì cần là một lập trình viên mới ra trường nhưng có kỹ năng tốt và biết cách xây dưng những sản phẩm hữu ích theo nhu cầu thị trường, bạn dễ dàng được nhận sau một buổi phỏng vấn. Thật tuyệt phải không?</p>

<p style="text-align: justify;">Thế giới lập trình vô cùng đa dạng về ngôn ngữ phát triển. Bạn chỉ cần xác định mục tiêu muốn theo đuổi để chọn được ngôn ngữ phù hợp. Tuy nhiên, dù chọn ngôn ngữ nào, bạn cũng cần biết tới SQL Server.</p>

<p style="text-align: justify;">Học SQL Server không khó. Hãy bắt đầu từ những kiến thức cơ bản nhất. Ở bài viết này là toán tử UNION trong SQL Server.</p>

<p style="text-align: justify;">Toán tử UNION ALL chọn các trường từ hai hoặc nhiều bảng tương tự như UNION. Tuy nhiên, khác UNION, UNION ALL không bỏ qua các trường trùng lặp.</p>

<p style="text-align: justify;">Toán tử UNION được dùng để kết hợp 2 bộ kết quả từ 2 hoặc nhiều lệnh&nbsp;SELECT. Nó sẽ xóa các hàng trùng trong các lệnh SELECT này.</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</h3>

<pre id="pre0" style="text-align: justify;">
SELECT bieu_thuc1, bieu_thuc2, … bieu_thucnFROM bang&#91;WHERE dieu_kien&#93;UNIONSELECT 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>

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

<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 xóa các hàng trùng nhau.</li>
	<li style="text-align: justify;">Xem thêm toán tử&nbsp;UNION ALL.</li>
</ul>

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

<p style="text-align: justify;">Ví dụ toán tử UNION trả về 1 trường từ nhiều lệnh SELECT (và các trường có cùng kiểu dữ liệu).</p>

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

<p style="text-align: justify;">Ở ví dụ trên, nếu có sanpham_id nào xuất hiện ở cả bảng sanpham và hangtonkho, thì sẽ chỉ còn 1 sanpham_id xuất hiện trong bộ kết quả. Nếu không muốn loại bỏ bản ghi trùng, hãy dùng toán tử UNION ALL.</p>

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

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

<pre id="pre2" style="text-align: justify;">
SELECT danhba_id, danhba_tenFROM danhbaWHERE ten_trang = ‘QuanTriMang.com’UNIONSELECT 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;">Áp dụng UNION cho nhiều hơn hai truy vấn</h2>

<p style="text-align: justify;">Bạn có thể dùng UNION để kết hợp kết quả của nhiều truy vấn nếu cần. Ví dụ một bảng về các loài vật nuôi, bạn có thể làm như sau:</p>

<pre id="pre3" style="text-align: justify;">
SELECT DogName AS PetName
FROM Dogs
UNION ALL
SELECT CatName
FROM Cats
UNION ALL
SELECT BirdName
FROM Birds;</pre>

<h2 style="text-align: justify;">So sánh SQL UNION và UNION ALL</h2>

<table border="0" cellpadding="2" cellspacing="2">
	<tbody>
		<tr>
			<td style="text-align: justify;"><strong>SQL UNION</strong></td>
			<td style="text-align: justify;"><strong>SQL UNION ALL</strong></td>
		</tr>
		<tr>
			<td style="text-align: justify;">Nó chỉ trả về các hàng riêng biệt từ tập kết quả của hai truy vấn.</td>
			<td style="text-align: justify;">Nó trả về những giá trị trùng lặp từ tập kết quả của hai truy vấn.</td>
		</tr>
		<tr>
			<td style="text-align: justify;">Chậm hơn so với toán tử&nbsp;<code>UNION ALL</code>.</td>
			<td style="text-align: justify;">Triển khai nhanh vì không cần bộ lọc tập kết quả bằng cách loại bỏ các giá trị trùng lặp.</td>
		</tr>
	</tbody>
</table>

<p style="text-align: justify;">Để cải thiện hiệu suất, bạn nên dùng UNION ALL khi biết các trường lựa chọn sẽ chỉ có những giá trị độc đáo.</p>

<h2 style="text-align: justify;">So sánh SQL UNION và SQL JOIN</h2>

<table border="0" cellpadding="2" cellspacing="2">
	<tbody>
		<tr>
			<td style="text-align: justify;"><strong>SQL JOIN</strong></td>
			<td style="text-align: justify;"><strong>SQL UNION</strong></td>
		</tr>
		<tr>
			<td style="text-align: justify;">Nó được dùng để kết hợp dữ liệu vào các cột mới từ những bảng khác nhau.</td>
			<td style="text-align: justify;">Nó được dùng để kết hợp dữ liệu vào các hàng mới từ kết quả của những truy vấn khác nhau.</td>
		</tr>
		<tr>
			<td style="text-align: justify;">Nó dùng cột thông dụng ở cả hai bảng để tìm nạp dữ liệu.</td>
			<td style="text-align: justify;">Nó chọn dữ liệu từ hai bảng và kết hợp chúng trong kết quả.</td>
		</tr>
		<tr>
			<td style="text-align: justify;">Thoải mái chia cột trong bảng.</td>
			<td style="text-align: justify;">Số lượng cột phải giống nhau ở cả hai bảng.</td>
		</tr>
		<tr>
			<td style="text-align: justify;">Kiểu cột dữ liệu có thể khác nhau.</td>
			<td style="text-align: justify;">Kiểu dữ liệu của cột lý tưởng là phải giống nhau (ngoại trừ một số database như SQLite).</td>
		</tr>
	</tbody>
</table>

<div style="text-align: justify;">&nbsp;</div>
		</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-trong-sql-server-9196.html" title="Toán tử UNION trong SQL Server">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/toan-tu-union-trong-sql-server-9196.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://www.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://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.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://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.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>