<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL 教學 &#8211; 小人物看世界</title>
	<atom:link href="https://blog.che-ya.com/category/sql-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.che-ya.com</link>
	<description>軟體工程師的技術筆記</description>
	<lastBuildDate>Thu, 02 Apr 2026 23:16:18 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.che-ya.com/wp-content/uploads/2021/08/cropped-APP_icon-32x32.png</url>
	<title>SQL 教學 &#8211; 小人物看世界</title>
	<link>https://blog.che-ya.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>SQL 聚合函數</title>
		<link>https://blog.che-ya.com/sql-aggregate-functions/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Thu, 26 Mar 2026 10:16:56 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=426</guid>

					<description><![CDATA[聚合函數是對一組值執行計算，並返回單一值的函數。聚合函數經常與 SELECT 語句的 GROUP BY 子句一同使用。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">聚合函數 (SQL Aggregate Functions)</h2>



<p>這篇 SQL 聚合函數教學將帶你認識聚合函數的基本概念。聚合函數是對一組值執行計算，並返回單一值的函數。聚合函數經常與 <a href="https://blog.che-ya.com/sql-select/">SELECT</a> 語句的 GROUP BY 子句一同使用。</p>



<p>以下是 SQL 中常用的聚合函數：</p>



<figure class="wp-block-table"><table><thead><tr><th>函數</th><th>說明</th></tr></thead><tbody><tr><td>COUNT()</td><td>計算列數</td></tr><tr><td>SUM()</td><td>計算總和</td></tr><tr><td>AVG()</td><td>計算平均值</td></tr><tr><td>MIN()</td><td>取得最小值</td></tr><tr><td>MAX()</td><td>取得最大值</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">聚合函數語法 (SQL Aggregate Functions Syntax)</h2>



<pre class="wp-block-code"><code class="">SELECT aggregate_function(column_name)
FROM table_name
WHERE condition;</code></pre>



<h2 class="wp-block-heading">聚合函數用法 (Example)</h2>



<p>假設我們有一個員工資料表 employees 如下（若需了解如何建立資料表，請參考 <a href="https://blog.che-ya.com/create-table/">CREATE TABLE</a> 教學）：</p>



<figure class="wp-block-table"><table><thead><tr><th>Name</th><th>Department</th><th>Salary</th></tr></thead><tbody><tr><td>張一</td><td>業務部</td><td>35000</td></tr><tr><td>王二</td><td>資訊部</td><td>42000</td></tr><tr><td>李三</td><td>業務部</td><td>38000</td></tr><tr><td>趙四</td><td>資訊部</td><td>45000</td></tr><tr><td>陳五</td><td>人資部</td><td>40000</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">COUNT() 函數</h3>



<p>COUNT() 用來計算符合條件的資料列數。</p>



<pre class="wp-block-code"><code class="">SELECT COUNT(*) AS total_employees
FROM employees;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>total_employees</th></tr></thead><tbody><tr><td>5</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">SUM() 函數</h3>



<p>SUM() 用來計算數值欄位的總和。</p>



<pre class="wp-block-code"><code class="">SELECT SUM(Salary) AS total_salary
FROM employees;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>total_salary</th></tr></thead><tbody><tr><td>200000</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">AVG() 函數</h3>



<p>AVG() 用來計算數值欄位的平均值。</p>



<pre class="wp-block-code"><code class="">SELECT AVG(Salary) AS avg_salary
FROM employees;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>avg_salary</th></tr></thead><tbody><tr><td>40000</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">MIN() 函數</h3>



<p>MIN() 用來取得欄位中的最小值。</p>



<pre class="wp-block-code"><code class="">SELECT MIN(Salary) AS min_salary
FROM employees;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>min_salary</th></tr></thead><tbody><tr><td>35000</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">MAX() 函數</h3>



<p>MAX() 用來取得欄位中的最大值。</p>



<pre class="wp-block-code"><code class="">SELECT MAX(Salary) AS max_salary
FROM employees;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>max_salary</th></tr></thead><tbody><tr><td>45000</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">搭配 GROUP BY 使用</h3>



<p>聚合函數經常搭配 <a href="https://blog.che-ya.com/sql-group-by/">GROUP BY</a> 子句來對分組後的資料進行統計計算。GROUP BY 會將資料依照指定的欄位進行分組，再對每個分組分別執行聚合函數，最終回傳每個分組的計算結果。</p>



<pre class="wp-block-code"><code class="">SELECT Department, COUNT(*) AS headcount, AVG(Salary) AS avg_salary
FROM employees
GROUP BY Department;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>Department</th><th>headcount</th><th>avg_salary</th></tr></thead><tbody><tr><td>業務部</td><td>2</td><td>36500</td></tr><tr><td>資訊部</td><td>2</td><td>43500</td></tr><tr><td>人資部</td><td>1</td><td>40000</td></tr></tbody></table></figure>



<p>GROUP BY 的基本語法如下，將想要分組的欄位放在 GROUP BY 後方，並在 SELECT 中搭配聚合函數：</p>



<pre class="wp-block-code"><code class="">SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name;</code></pre>



<p>例如，想要查詢每個部門的員工人數與最高薪資，可以這樣寫：</p>



<pre class="wp-block-code"><code class="">SELECT Department, COUNT(*) AS headcount, MAX(Salary) AS max_salary
FROM employees
GROUP BY Department;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Department</th><th>headcount</th><th>max_salary</th></tr></thead><tbody><tr><td>業務部</td><td>2</td><td>40000</td></tr><tr><td>資訊部</td><td>2</td><td>45000</td></tr><tr><td>人資部</td><td>1</td><td>40000</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">搭配 HAVING 篩選分組結果</h3>



<p>當我們需要對分組後的聚合結果進行篩選時，不能使用 <a href="https://blog.che-ya.com/sql-where/">WHERE</a>，因為 WHERE 是在分組前篩選資料列。此時需要使用 HAVING 子句，它可以針對聚合函數的計算結果進行條件篩選。</p>



<pre class="wp-block-code"><code class="">SELECT Department, COUNT(*) AS headcount, AVG(Salary) AS avg_salary
FROM employees
GROUP BY Department
HAVING COUNT(*) >= 2;</code></pre>



<p>查詢結果如下（只顯示員工人數 >= 2 的部門）：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Department</th><th>headcount</th><th>avg_salary</th></tr></thead><tbody><tr><td>業務部</td><td>2</td><td>36500</td></tr><tr><td>資訊部</td><td>2</td><td>43500</td></tr></tbody></table></figure>



<p>想了解更多 GROUP BY 的進階用法，請參閱 <a href="https://blog.che-ya.com/sql-group-by/">SQL GROUP BY</a> 完整教學。</p>



<p></p>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-group-by/">SQL GROUP BY</a></li>



<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery</a></li>



<li><a href="https://blog.che-ya.com/sql-case/">SQL CASE</a></li>



<li><a href="https://blog.che-ya.com/sql-order-by/">SQL ORDER BY</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-date/">SQL Date</a></li>



<li><a href="https://blog.che-ya.com/sql-exists/">SQL EXISTS</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-aggregate-functions%2F&amp;linkname=SQL%20%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B8" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-aggregate-functions%2F&amp;linkname=SQL%20%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B8" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-aggregate-functions%2F&amp;linkname=SQL%20%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B8" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-aggregate-functions%2F&#038;title=SQL%20%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B8" data-a2a-url="https://blog.che-ya.com/sql-aggregate-functions/" data-a2a-title="SQL 聚合函數"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Date</title>
		<link>https://blog.che-ya.com/sql-date/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 13 Dec 2024 06:31:48 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=187</guid>

					<description><![CDATA[一般資料庫都有提供表示日期的資料型態 (Date Data Types)。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">日期資料型態 (SQL Date Data Types)</h2>



<p>這篇 SQL 日期函數教學介紹資料庫中常用的日期資料型態與日期函數。一般資料庫都有提供表示日期的資料型態 (Date Data Types)，搭配 <a href="https://blog.che-ya.com/sql-select/">SELECT</a> 語句可以靈活查詢日期相關資料。</p>



<h2 class="wp-block-heading">MySQL 日期資料型態</h2>



<p>MySQL 中常用的日期資料型態如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>資料型態</th><th>格式</th><th>說明</th></tr></thead><tbody><tr><td>DATE</td><td>YYYY-MM-DD</td><td>日期</td></tr><tr><td>DATETIME</td><td>YYYY-MM-DD HH:MI:SS</td><td>日期與時間</td></tr><tr><td>TIMESTAMP</td><td>YYYY-MM-DD HH:MI:SS</td><td>時間戳記</td></tr><tr><td>YEAR</td><td>YYYY 或 YY</td><td>年份</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">SQL Date 函數 (SQL Date Functions)</h2>



<p>以下是一些常用的 SQL 日期函數：</p>



<figure class="wp-block-table"><table><thead><tr><th>函數</th><th>說明</th></tr></thead><tbody><tr><td>NOW()</td><td>返回目前的日期與時間</td></tr><tr><td>CURDATE()</td><td>返回目前的日期</td></tr><tr><td>CURTIME()</td><td>返回目前的時間</td></tr><tr><td>DATE()</td><td>提取日期部分</td></tr><tr><td>YEAR()</td><td>提取年份</td></tr><tr><td>MONTH()</td><td>提取月份</td></tr><tr><td>DAY()</td><td>提取日</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">日期函數用法 (Example)</h2>



<p>假設我們有一個訂單資料表 orders 如下（若需了解如何建立資料表，請參考 <a href="https://blog.che-ya.com/create-table/">CREATE TABLE</a> 教學）：</p>



<figure class="wp-block-table"><table><thead><tr><th>OrderId</th><th>ProductName</th><th>OrderDate</th></tr></thead><tbody><tr><td>1</td><td>蘋果</td><td>2024-11-01</td></tr><tr><td>2</td><td>香蕉</td><td>2024-11-15</td></tr><tr><td>3</td><td>橘子</td><td>2024-12-01</td></tr></tbody></table></figure>



<p>我們可以使用 DATE 函數搭配 <a href="https://blog.che-ya.com/sql-where/">WHERE</a> 子句來查詢特定日期的訂單：</p>



<pre class="wp-block-code"><code class="">SELECT * FROM orders
WHERE DATE(OrderDate) = '2024-11-01';</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>OrderId</th><th>ProductName</th><th>OrderDate</th></tr></thead><tbody><tr><td>1</td><td>蘋果</td><td>2024-11-01</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">各資料庫日期函數差異 (Date Functions Across Databases)</h2>



<p>不同的資料庫系統在日期函數的語法上有所差異，以下整理了 MySQL、SQL Server、PostgreSQL 和 Oracle 中常用的日期操作函數，幫助你在不同資料庫環境下靈活運用。</p>



<h3 class="wp-block-heading">日期格式化 (Date Formatting)</h3>



<p>將日期轉換為指定格式的字串，各資料庫的語法如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>資料庫</th><th>語法</th><th>範例</th></tr></thead><tbody><tr><td>MySQL</td><td>DATE_FORMAT(date, format)</td><td>DATE_FORMAT(NOW(), &#8216;%Y-%m-%d&#8217;)</td></tr><tr><td>SQL Server</td><td>FORMAT(date, format)</td><td>FORMAT(GETDATE(), &#8216;yyyy-MM-dd&#8217;)</td></tr><tr><td>PostgreSQL</td><td>TO_CHAR(date, format)</td><td>TO_CHAR(NOW(), &#8216;YYYY-MM-DD&#8217;)</td></tr><tr><td>Oracle</td><td>TO_CHAR(date, format)</td><td>TO_CHAR(SYSDATE, &#8216;YYYY-MM-DD&#8217;)</td></tr></tbody></table></figure>



<p>以 MySQL 為例，使用 DATE_FORMAT 將訂單日期格式化為「年/月」的格式：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql">SELECT ProductName, DATE_FORMAT(OrderDate, '%Y/%m') AS FormattedDate
FROM orders;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>ProductName</th><th>FormattedDate</th></tr></thead><tbody><tr><td>蘋果</td><td>2024/11</td></tr><tr><td>香蕉</td><td>2024/11</td></tr><tr><td>橘子</td><td>2024/12</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">日期加減運算 (Date Addition)</h3>



<p>在日期上加上或減去一段時間間隔，各資料庫的語法差異較大：</p>



<figure class="wp-block-table"><table><thead><tr><th>資料庫</th><th>語法</th><th>加 7 天的範例</th></tr></thead><tbody><tr><td>MySQL</td><td>DATE_ADD(date, INTERVAL n unit)</td><td>DATE_ADD(&#8216;2024-11-01&#8217;, INTERVAL 7 DAY)</td></tr><tr><td>SQL Server</td><td>DATEADD(unit, n, date)</td><td>DATEADD(DAY, 7, &#8216;2024-11-01&#8217;)</td></tr><tr><td>PostgreSQL</td><td>date + INTERVAL &#8216;n unit&#8217;</td><td>&#8216;2024-11-01&#8217;::date + INTERVAL &#8216;7 days&#8217;</td></tr><tr><td>Oracle</td><td>date + n（天數）</td><td>TO_DATE(&#8216;2024-11-01&#8217;, &#8216;YYYY-MM-DD&#8217;) + 7</td></tr></tbody></table></figure>



<p>以 MySQL 為例，查詢每筆訂單 7 天後的日期：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql">SELECT ProductName, OrderDate,
       DATE_ADD(OrderDate, INTERVAL 7 DAY) AS DeliveryDate
FROM orders;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>ProductName</th><th>OrderDate</th><th>DeliveryDate</th></tr></thead><tbody><tr><td>蘋果</td><td>2024-11-01</td><td>2024-11-08</td></tr><tr><td>香蕉</td><td>2024-11-15</td><td>2024-11-22</td></tr><tr><td>橘子</td><td>2024-12-01</td><td>2024-12-08</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">日期差異計算 (Date Difference)</h3>



<p>計算兩個日期之間的差異，各資料庫的寫法也不盡相同：</p>



<figure class="wp-block-table"><table><thead><tr><th>資料庫</th><th>語法</th><th>說明</th></tr></thead><tbody><tr><td>MySQL</td><td>DATEDIFF(date1, date2)</td><td>回傳天數差（date1 &#8211; date2）</td></tr><tr><td>SQL Server</td><td>DATEDIFF(unit, start, end)</td><td>可指定回傳單位（DAY、MONTH、YEAR 等）</td></tr><tr><td>PostgreSQL</td><td>date1 &#8211; date2</td><td>直接相減，回傳天數</td></tr><tr><td>Oracle</td><td>date1 &#8211; date2</td><td>直接相減，回傳天數（含小數）</td></tr></tbody></table></figure>



<p>以 MySQL 為例，計算每筆訂單距離今天過了幾天：</p>



<pre class="wp-block-code"><code lang="sql" class="language-sql">SELECT ProductName, OrderDate,
       DATEDIFF(CURDATE(), OrderDate) AS DaysAgo
FROM orders;</code></pre>



<p>了解各資料庫之間日期函數的差異，可以幫助你在切換不同資料庫環境時更快速地撰寫正確的 SQL 語句。建議在開發時先確認目標資料庫的日期函數語法，避免因語法差異導致查詢錯誤。</p>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-case/">SQL CASE</a></li>



<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-between/">SQL BETWEEN</a></li>



<li><a href="https://blog.che-ya.com/sql-aggregate-functions/">SQL 聚合函數</a></li>



<li><a href="https://blog.che-ya.com/sql-order-by/">SQL ORDER BY</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-date%2F&amp;linkname=SQL%20Date" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-date%2F&amp;linkname=SQL%20Date" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-date%2F&amp;linkname=SQL%20Date" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-date%2F&#038;title=SQL%20Date" data-a2a-url="https://blog.che-ya.com/sql-date/" data-a2a-title="SQL Date"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL CASE</title>
		<link>https://blog.che-ya.com/sql-case/</link>
					<comments>https://blog.che-ya.com/sql-case/#comments</comments>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 10 Dec 2024 06:31:48 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=186</guid>

					<description><![CDATA[CASE 類似於程式語言裡的 if/then/else 語句，用來作邏輯判斷。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">CASE 關鍵字 (SQL CASE Keyword)</h2>



<p>這篇 SQL CASE 條件判斷教學介紹 CASE 關鍵字的用法。CASE 類似於程式語言裡的 if/then/else 語句，用來在 <a href="https://blog.che-ya.com/sql-select/">SELECT</a> 查詢中作邏輯判斷。</p>



<h2 class="wp-block-heading">CASE 語法 (SQL CASE Syntax)</h2>



<pre class="wp-block-code"><code class="">CASE
    WHEN condition THEN result
    [WHEN···]
    [ELSE result]
END;</code></pre>



<p>或是：</p>



<pre class="wp-block-code"><code class="">CASE expression
    WHEN value THEN result
    [WHEN···]
    [ELSE result]
END;</code></pre>



<p>若省略了 ELSE 子句且洽無相符的條件則會返回 NULL。CASE 可搭配 <a href="https://blog.che-ya.com/sql-where/">WHERE</a>、<a href="https://blog.che-ya.com/sql-order-by/">ORDER BY</a> 等子句使用。</p>



<h2 class="wp-block-heading">CASE 關鍵字用法 (Example)</h2>



<p>假設我們作一個問卷調查：您喜歡這個網站嗎？1.喜歡 2.不喜歡 3.還OK</p>



<p>問卷結果資料表 questionnaire 如下（若需了解如何建立資料表，請參考 <a href="https://blog.che-ya.com/create-table/">CREATE TABLE</a> 教學）：</p>



<figure class="wp-block-table"><table><thead><tr><th>Name</th><th>Answer</th></tr></thead><tbody><tr><td>張一</td><td>1</td></tr><tr><td>王二</td><td>2</td></tr><tr><td>李三</td><td>3</td></tr></tbody></table></figure>



<p>我們可以作以下這個 SQL 查詢：</p>



<pre class="wp-block-code"><code class="">SELECT Name,
    CASE Answer
        WHEN 1 THEN '喜歡'
        WHEN 2 THEN '不喜歡'
        WHEN 3 THEN '還OK'
    END AS Answer
FROM questionnaire;</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>Name</th><th>Answer</th></tr></thead><tbody><tr><td>張一</td><td>喜歡</td></tr><tr><td>王二</td><td>不喜歡</td></tr><tr><td>李三</td><td>還OK</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery</a></li>



<li><a href="https://blog.che-ya.com/sql-exists/">SQL EXISTS</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-aggregate-functions/">SQL 聚合函數</a></li>



<li><a href="https://blog.che-ya.com/sql-date/">SQL Date</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-in/">SQL IN</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-case%2F&amp;linkname=SQL%20CASE" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-case%2F&amp;linkname=SQL%20CASE" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-case%2F&amp;linkname=SQL%20CASE" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-case%2F&#038;title=SQL%20CASE" data-a2a-url="https://blog.che-ya.com/sql-case/" data-a2a-title="SQL CASE"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.che-ya.com/sql-case/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>SQL EXISTS</title>
		<link>https://blog.che-ya.com/sql-exists/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 06 Dec 2024 06:31:48 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=184</guid>

					<description><![CDATA[EXISTS 運算子可以連接子查詢，用來判斷子查詢是否有返回的結果，如果有結果返回則為真、否則為假。若 EXISTS 為真，就會繼續執行外查詢中的 SQL；若 EXISTS 為假，則整個 SQL 查詢就不會返回任何結果。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">EXISTS 運算子 (SQL EXISTS Operator)</h2>



<p>這篇 SQL EXISTS 子查詢教學介紹 EXISTS 運算子的用法。EXISTS 運算子可以連接<a href="https://blog.che-ya.com/sql-subquery/">子查詢</a>，用來判斷子查詢是否有返回的結果，如果有結果返回則為真、否則為假。若 EXISTS 為真，就會繼續執行外查詢中的 <a href="https://blog.che-ya.com/sql-select/">SQL 查詢</a>；若 EXISTS 為假，則整個 SQL 查詢就不會返回任何結果。</p>



<h2 class="wp-block-heading">EXISTS 語法 (SQL EXISTS Syntax)</h2>



<pre class="wp-block-code"><code class="">SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);</code></pre>



<h2 class="wp-block-heading">EXISTS 用法 (Example)</h2>



<p>假設我們有兩個資料表，customers 和 orders（若需了解如何建立資料表，請參考 <a href="https://blog.che-ya.com/create-table/">CREATE TABLE</a> 教學）：</p>



<p>customers 資料表：</p>



<figure class="wp-block-table"><table><thead><tr><th>CustomerID</th><th>CustomerName</th></tr></thead><tbody><tr><td>1</td><td>張一</td></tr><tr><td>2</td><td>王二</td></tr><tr><td>3</td><td>李三</td></tr></tbody></table></figure>



<p>orders 資料表：</p>



<figure class="wp-block-table"><table><thead><tr><th>OrderID</th><th>CustomerID</th><th>Product</th></tr></thead><tbody><tr><td>1</td><td>1</td><td>蘋果</td></tr><tr><td>2</td><td>1</td><td>香蕉</td></tr><tr><td>3</td><td>3</td><td>橘子</td></tr></tbody></table></figure>



<p>使用 EXISTS 搭配 <a href="https://blog.che-ya.com/sql-where/">WHERE</a> 子句查詢有下過訂單的客戶：</p>



<pre class="wp-block-code"><code class="">SELECT CustomerName
FROM customers c
WHERE EXISTS
(SELECT 1 FROM orders o WHERE o.CustomerID = c.CustomerID);</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>CustomerName</th></tr></thead><tbody><tr><td>張一</td></tr><tr><td>李三</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery</a></li>



<li><a href="https://blog.che-ya.com/sql-case/">SQL CASE</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-in/">SQL IN</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-aggregate-functions/">SQL 聚合函數</a></li>



<li><a href="https://blog.che-ya.com/sql-date/">SQL Date</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-exists%2F&amp;linkname=SQL%20EXISTS" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-exists%2F&amp;linkname=SQL%20EXISTS" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-exists%2F&amp;linkname=SQL%20EXISTS" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-exists%2F&#038;title=SQL%20EXISTS" data-a2a-url="https://blog.che-ya.com/sql-exists/" data-a2a-title="SQL EXISTS"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Subquery</title>
		<link>https://blog.che-ya.com/sql-subquery/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 03 Dec 2024 06:31:48 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=185</guid>

					<description><![CDATA[我們可以將一個 SQL 查詢語句塞入另一個 SQL 查詢語句中，這就是子查詢 (subquery)。子查詢是一個查詢內的查詢，我們可以用來連接資料表，或在不能使用單一語句來完成的查詢時我們就需要用到子查詢。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">子查詢 (SQL Subquery)</h2>



<p>我們可以將一個 SQL 查詢語句塞入另一個 SQL 查詢語句中，這就是子查詢 (subquery)。子查詢是一個查詢內的查詢，我們可以用來連接資料表，或在不能使用單一語句來完成的查詢時我們就需要用到子查詢。</p>



<h2 class="wp-block-heading">子查詢語法 (SQL Subquery Syntax)</h2>



<pre class="wp-block-code"><code class="">SELECT column_name(s)
FROM table_name
WHERE column_name operator
(SELECT column_name FROM table_name WHERE condition);</code></pre>



<h2 class="wp-block-heading">子查詢用法 (Example)</h2>



<p>假設我們有一個員工資料表 employees：</p>



<figure class="wp-block-table"><table><thead><tr><th>Name</th><th>Department</th><th>Salary</th></tr></thead><tbody><tr><td>張一</td><td>業務部</td><td>35000</td></tr><tr><td>王二</td><td>資訊部</td><td>42000</td></tr><tr><td>李三</td><td>業務部</td><td>38000</td></tr><tr><td>趙四</td><td>資訊部</td><td>45000</td></tr></tbody></table></figure>



<p>查詢薪水高於平均薪水的員工：</p>



<pre class="wp-block-code"><code class="">SELECT Name, Salary
FROM employees
WHERE Salary &gt; (SELECT AVG(Salary) FROM employees);</code></pre>



<p>查詢結果如下：</p>



<figure class="wp-block-table"><table><thead><tr><th>Name</th><th>Salary</th></tr></thead><tbody><tr><td>王二</td><td>42000</td></tr><tr><td>趙四</td><td>45000</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">使用 CTE 改寫子查詢 (Common Table Expression)</h2>



<p>除了子查詢之外，SQL 還提供了 CTE（Common Table Expression，通用表示式）作為替代寫法。CTE 使用 <code>WITH</code> 關鍵字來定義一個暫時的命名結果集，讓查詢變得更易讀、更好維護。當子查詢變得複雜或需要重複使用時，CTE 是更好的選擇。</p>



<h3 class="wp-block-heading">CTE 語法 (CTE Syntax)</h3>



<pre class="wp-block-code"><code class="">WITH cte_name AS (
    SELECT column_name(s)
    FROM table_name
    WHERE condition
)
SELECT column_name(s)
FROM cte_name;</code></pre>



<p><code>WITH</code> 關鍵字用來定義 CTE，<code>cte_name</code> 是我們給這個暫時結果集的名稱，括號內的查詢就是 CTE 的定義。定義完成後，我們就可以在主查詢中像使用資料表一樣來使用 CTE。</p>



<h3 class="wp-block-heading">CTE 用法範例 (CTE Example)</h3>



<p>我們可以用 CTE 來改寫前面的子查詢範例，查詢薪水高於平均薪水的員工：</p>



<pre class="wp-block-code"><code class="">WITH avg_salary AS (
    SELECT AVG(Salary) AS AvgSalary
    FROM employees
)
SELECT Name, Salary
FROM employees, avg_salary
WHERE Salary > avg_salary.AvgSalary;</code></pre>



<p>這個查詢與子查詢的結果完全相同，但 CTE 的寫法讓查詢的邏輯更清晰：先計算平均薪水，再用這個結果來篩選員工。</p>



<h3 class="wp-block-heading">多個 CTE 的使用 (Multiple CTEs)</h3>



<p>CTE 還有一個強大的優點，就是可以在同一個查詢中定義多個 CTE，用逗號分隔：</p>



<pre class="wp-block-code"><code class="">WITH dept_avg AS (
    SELECT Department, AVG(Salary) AS AvgSalary
    FROM employees
    GROUP BY Department
),
overall_avg AS (
    SELECT AVG(Salary) AS OverallAvg
    FROM employees
)
SELECT d.Department, d.AvgSalary, o.OverallAvg
FROM dept_avg d, overall_avg o;</code></pre>



<p>這個範例同時計算了各部門的平均薪水和全體平均薪水，如果用子查詢寫會變得難以閱讀，但用 CTE 就能清楰地分步驟處理。</p>



<h3 class="wp-block-heading">子查詢與 CTE 的比較</h3>



<figure class="wp-block-table"><table><thead><tr><th>比較項目</th><th>子查詢 (Subquery)</th><th>CTE (WITH)</th></tr></thead><tbody><tr><td>可讀性</td><td>巢狀結構，複雜時較難閱讀</td><td>分步定義，邏輯清晰</td></tr><tr><td>重複使用</td><td>需要重複寫同一段查詢</td><td>定義一次即可多次引用</td></tr><tr><td>遞迴查詢</td><td>不支援</td><td>支援遞迴 CTE</td></tr><tr><td>適用場景</td><td>簡單的單次查詢</td><td>複雜的多步驟查詢</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-exists/">SQL EXISTS</a></li>



<li><a href="https://blog.che-ya.com/sql-case/">SQL CASE</a></li>



<li><a href="https://blog.che-ya.com/sql-aggregate-functions/">SQL 聚合函數</a></li>



<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-in/">SQL IN</a></li>



<li><a href="https://blog.che-ya.com/sql-union/">SQL UNION</a></li>



<li><a href="https://blog.che-ya.com/sql-intersect/">SQL INTERSECT</a></li>



<li><a href="https://blog.che-ya.com/sql-minus/">SQL MINUS</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-subquery%2F&amp;linkname=SQL%20Subquery" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-subquery%2F&amp;linkname=SQL%20Subquery" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-subquery%2F&amp;linkname=SQL%20Subquery" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-subquery%2F&#038;title=SQL%20Subquery" data-a2a-url="https://blog.che-ya.com/sql-subquery/" data-a2a-title="SQL Subquery"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL MINUS</title>
		<link>https://blog.che-ya.com/sql-minus/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 29 Nov 2024 06:31:32 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=182</guid>

					<description><![CDATA[當 MINUS 運算子結合了兩個 SELECT 查詢語句，它會將 (第一個查詢結果集) 減去 (同時存在於第一個查詢結果集與第二個查詢結果集的資料紀錄)，然後返回其結果。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">MINUS 運算子 (SQL MINUS Operator)</h2>



<p>當 MINUS 運算子結合了兩個 SELECT 查詢語句，它會將 (第一個查詢結果集) 減去 (同時存在於第一個查詢結果集與第二個查詢結果集的資料紀錄)，然後返回其結果。</p>



<h2 class="wp-block-heading">MINUS 語法 (SQL MINUS Syntax)</h2>



<pre class="wp-block-code"><code class="">SELECT column_name(s) FROM table1
MINUS
SELECT column_name(s) FROM table2;</code></pre>



<p>注意：MySQL 不支援 MINUS，可以使用 NOT IN 或 LEFT JOIN 來達到相同效果。</p>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-union/">SQL UNION</a></li>



<li><a href="https://blog.che-ya.com/sql-intersect/">SQL INTERSECT</a></li>



<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-in/">SQL IN</a></li>



<li><a href="https://blog.che-ya.com/sql-exists/">SQL EXISTS</a></li>



<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery</a></li>



<li><a href="https://blog.che-ya.com/sql-aggregate-functions/">SQL 聚合函數</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-minus%2F&amp;linkname=SQL%20MINUS" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-minus%2F&amp;linkname=SQL%20MINUS" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-minus%2F&amp;linkname=SQL%20MINUS" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-minus%2F&#038;title=SQL%20MINUS" data-a2a-url="https://blog.che-ya.com/sql-minus/" data-a2a-title="SQL MINUS"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL INTERSECT</title>
		<link>https://blog.che-ya.com/sql-intersect/</link>
					<comments>https://blog.che-ya.com/sql-intersect/#comments</comments>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 26 Nov 2024 06:31:00 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=181</guid>

					<description><![CDATA[相對於 UNION 像是 OR (聯集)，INTERSECT 運算子則像是 AND (交集)，如果紀錄存在於第一個查詢結果集內同時亦存在於第二個查詢結果集內時，才會被取出。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">INTERSECT 運算子 (SQL INTERSECT Operator)</h2>



<p>相對於 UNION 像是 OR (聯集)，INTERSECT 運算子則像是 AND (交集)，如果紀錄存在於第一個查詢結果集內同時亦存在於第二個查詢結果集內時，才會被取出。</p>



<h2 class="wp-block-heading">INTERSECT 語法 (SQL INTERSECT Syntax)</h2>



<pre class="wp-block-code"><code class="">SELECT column_name(s) FROM table1
INTERSECT
SELECT column_name(s) FROM table2;</code></pre>



<p>注意：MySQL 不直接支援 INTERSECT，可以使用 INNER JOIN 或 IN 子查詢來達到相同效果。</p>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-union/">SQL UNION</a></li>



<li><a href="https://blog.che-ya.com/sql-minus/">SQL MINUS</a></li>



<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-in/">SQL IN</a></li>



<li><a href="https://blog.che-ya.com/sql-exists/">SQL EXISTS</a></li>



<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery</a></li>



<li><a href="https://blog.che-ya.com/sql-aggregate-functions/">SQL 聚合函數</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-intersect%2F&amp;linkname=SQL%20INTERSECT" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-intersect%2F&amp;linkname=SQL%20INTERSECT" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-intersect%2F&amp;linkname=SQL%20INTERSECT" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-intersect%2F&#038;title=SQL%20INTERSECT" data-a2a-url="https://blog.che-ya.com/sql-intersect/" data-a2a-title="SQL INTERSECT"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.che-ya.com/sql-intersect/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL UNION</title>
		<link>https://blog.che-ya.com/sql-union/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 22 Nov 2024 06:31:39 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=183</guid>

					<description><![CDATA[UNION 運算子用來將兩個(以上) SQL 查詢的結果合併起來，而由 UNION 查詢中各別 SQL 語句所產生的欄位需要是相同的資料型別及順序。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">UNION 運算子 (SQL UNION Operator)</h2>



<p>UNION 運算子用來將兩個(以上) SQL 查詢的結果合併起來，而由 UNION 查詢中各別 SQL 語句所產生的欄位需要是相同的資料型別及順序。</p>



<h2 class="wp-block-heading">UNION 語法 (SQL UNION Syntax)</h2>



<pre class="wp-block-code"><code class="">SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;</code></pre>



<p>UNION 預設會去除重複的資料列。如果要保留重複的資料列，可以使用 UNION ALL：</p>



<pre class="wp-block-code"><code class="">SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;</code></pre>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-intersect/">SQL INTERSECT</a></li>



<li><a href="https://blog.che-ya.com/sql-minus/">SQL MINUS</a></li>



<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-select/">SQL SELECT</a></li>



<li><a href="https://blog.che-ya.com/sql-where/">SQL WHERE</a></li>



<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-in/">SQL IN</a></li>



<li><a href="https://blog.che-ya.com/sql-exists/">SQL EXISTS</a></li>



<li><a href="https://blog.che-ya.com/sql-subquery/">SQL Subquery</a></li>



<li><a href="https://blog.che-ya.com/sql-aggregate-functions/">SQL 聚合函數</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-union%2F&amp;linkname=SQL%20UNION" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-union%2F&amp;linkname=SQL%20UNION" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-union%2F&amp;linkname=SQL%20UNION" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-union%2F&#038;title=SQL%20UNION" data-a2a-url="https://blog.che-ya.com/sql-union/" data-a2a-title="SQL UNION"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL NATURAL JOIN</title>
		<link>https://blog.che-ya.com/sql-natural-join/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 19 Nov 2024 06:30:01 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=176</guid>

					<description><![CDATA[自然連接有 NATURAL JOIN、NATURAL LEFT JOIN、NATURAL RIGHT JOIN，兩個表格在進行 JOIN 時，加上 NATURAL 這個關鍵字之後，兩資料表之間同名的欄位會被自動結合在一起。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">自然連接 (SQL NATURAL JOIN)</h2>



<p>自然連接有 NATURAL JOIN、NATURAL LEFT JOIN、NATURAL RIGHT JOIN，兩個表格在進行 JOIN 時，加上 NATURAL 這個關鍵字之後，兩資料表之間同名的欄位會自動被拿來當作連接條件，不需要另外指定 ON 或 USING。</p>



<h2 class="wp-block-heading">NATURAL JOIN 語法 (Syntax)</h2>



<pre class="wp-block-code"><code class="">SELECT column_name(s)
FROM table1
NATURAL JOIN table2;</code></pre>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-cross-join/">SQL CROSS JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-full-join/">SQL FULL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-inner-join/">SQL INNER JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-left-join/">SQL LEFT JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-right-join/">SQL RIGHT JOIN</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-natural-join%2F&amp;linkname=SQL%20NATURAL%20JOIN" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-natural-join%2F&amp;linkname=SQL%20NATURAL%20JOIN" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-natural-join%2F&amp;linkname=SQL%20NATURAL%20JOIN" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-natural-join%2F&#038;title=SQL%20NATURAL%20JOIN" data-a2a-url="https://blog.che-ya.com/sql-natural-join/" data-a2a-title="SQL NATURAL JOIN"></a></p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL CROSS JOIN</title>
		<link>https://blog.che-ya.com/sql-cross-join/</link>
					<comments>https://blog.che-ya.com/sql-cross-join/#comments</comments>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Fri, 15 Nov 2024 06:28:54 +0000</pubDate>
				<category><![CDATA[資料查詢 DQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=174</guid>

					<description><![CDATA[交叉連接為兩個資料表間的笛卡兒乘積 (Cartesian product)，兩個資料表在結合時，不指定任何條件，即將兩個資料表中所有的可能排列組合出來，以下例而言 CROSS JOIN 出來的結果資料列數為 3×5=15 筆，因此，當有 WHERE、ON、USING 條件時不建議使用。]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">交叉連接 (SQL CROSS JOIN)</h2>



<p>交叉連接為兩個資料表間的笛卡兒乘積 (Cartesian product)，兩個資料表在結合時，不指定任何條件，即將兩個資料表中所有的可能排列組合出來。因此，當有 WHERE、ON、USING 條件時不建議使用。</p>



<h2 class="wp-block-heading">CROSS JOIN 語法 (Syntax)</h2>



<pre class="wp-block-code"><code class="">SELECT column_name(s)
FROM table1
CROSS JOIN table2;</code></pre>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/sql-join/">SQL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-full-join/">SQL FULL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-natural-join/">SQL NATURAL JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-tutorial/">SQL 教學</a></li>



<li><a href="https://blog.che-ya.com/sql-inner-join/">SQL INNER JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-left-join/">SQL LEFT JOIN</a></li>



<li><a href="https://blog.che-ya.com/sql-right-join/">SQL RIGHT JOIN</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-cross-join%2F&amp;linkname=SQL%20CROSS%20JOIN" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-cross-join%2F&amp;linkname=SQL%20CROSS%20JOIN" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fsql-cross-join%2F&amp;linkname=SQL%20CROSS%20JOIN" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fsql-cross-join%2F&#038;title=SQL%20CROSS%20JOIN" data-a2a-url="https://blog.che-ya.com/sql-cross-join/" data-a2a-title="SQL CROSS JOIN"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.che-ya.com/sql-cross-join/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
