group by語句,聚合函數與其餘語句(關係,執行過程)

<div class="htmledit_views">html

<p><span style="color:#ff0000;"><span style="color:#000000;">(</span>本文一切基於SQL SERVER 2008 R2</span>)</p> <p>&nbsp;</p> <p>先來看一個語句執行過程發生的錯誤提示:</p> <div class="dp-highlighter bg_html"><div class="bar"><div class="tools"><b>[html]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 504px; top: 452px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 504px; top: 452px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_10" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_10" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=10&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-xml"><li class="alt"><span><span>消息&nbsp;8120,級別&nbsp;16,狀態&nbsp;1,第&nbsp;2&nbsp;行&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;選擇列表中的列&nbsp;'SC.CNO'&nbsp;無效,由於該列沒有包含在聚合函數或&nbsp;GROUP&nbsp;BY&nbsp;子句中。&nbsp;&nbsp;</span></li></ol></div><pre class="html" name="code" style="display: none;"> 消息 8120,級別 16,狀態 1,第 2 行 選擇列表中的列 'SC.CNO' 無效,由於該列沒有包含在聚合函數或 GROUP BY 子句中。</pre> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 元數據庫爲Stu_C,含有三個表,S、SC、C。執行的語句以下:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p> <div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 493px; top: 623px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 493px; top: 623px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_11" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_11" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=11&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span>use&nbsp;Stu_C&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">select</span><span>&nbsp;SNO,cno,</span><span class="func">sum</span><span>(grade)&nbsp;sum_grade&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">from</span><span>&nbsp;SC&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">where</span><span>&nbsp;GRADE&gt;=60&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">group</span><span>&nbsp;</span><span class="keyword">by</span><span>&nbsp;SC.SNO&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">having</span><span>&nbsp;</span><span class="func">count</span><span>(CNO)&gt;=4&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">order</span><span>&nbsp;</span><span class="keyword">by</span><span>&nbsp;sum_grade&nbsp;</span><span class="keyword">desc</span><span>&nbsp;&nbsp;</span></span></li></ol></div><pre class="sql" name="code" style="display: none;"> use Stu_C select SNO,cno,sum(grade) sum_grade from SC where GRADE&gt;=60 group by SC.SNO having count(CNO)&gt;=4 order by sum_grade desc</pre><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 493px; top: 798px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 493px; top: 798px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_12" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_12" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=12&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span>&nbsp;目的是檢索選修4門課以上課程的學生總成績(排除不及格的),並按總成績的降序排列出來。根據提示咱們容易知道,</span><span class="keyword">select</span><span>語句中多了cno這個屬性列。若是將其去掉執&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;行以下:&nbsp;&nbsp;</span></li></ol></div><pre class="sql" name="code" style="display: none;"> &nbsp;目的是檢索選修4門課以上課程的學生總成績(排除不及格的),並按總成績的降序排列出來。根據提示咱們容易知道,select語句中多了cno這個屬性列。若是將其去掉執 行以下:</pre><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 493px; top: 901px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_4" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 493px; top: 901px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_13" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_13" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=13&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span>use&nbsp;Stu_C&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">select</span><span>&nbsp;SNO,</span><span class="func">sum</span><span>(grade)&nbsp;sum_grade&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">from</span><span>&nbsp;SC&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">where</span><span>&nbsp;GRADE&gt;=60&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">group</span><span>&nbsp;</span><span class="keyword">by</span><span>&nbsp;SC.SNO&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">having</span><span>&nbsp;</span><span class="func">count</span><span>(CNO)&gt;=4&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">order</span><span>&nbsp;</span><span class="keyword">by</span><span>&nbsp;sum_grade&nbsp;</span><span class="keyword">desc</span><span>&nbsp;&nbsp;</span></span></li></ol></div><pre class="sql" name="code" style="display: none;"> use Stu_C select SNO,sum(grade) sum_grade from SC where GRADE&gt;=60 group by SC.SNO having count(CNO)&gt;=4 order by sum_grade desc</pre><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 493px; top: 1076px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_5" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 493px; top: 1076px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_14" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_14" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=14&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span>結果:&nbsp;&nbsp;</span></span></li></ol></div><pre class="sql" name="code" style="display: none;"> 結果:</pre><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 493px; top: 1143px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_6" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 493px; top: 1143px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_15" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_15" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=15&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span>&nbsp;&nbsp;&nbsp;</span></span></li></ol></div><pre class="sql" name="code" style="display: none;"> &nbsp;</pre><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 493px; top: 1210px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_7" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 493px; top: 1210px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_16" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_16" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=16&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span>分析及結論:&nbsp;&nbsp;</span></span></li></ol></div><pre class="sql" name="code" style="display: none;">分析及結論:</pre> <blockquote dir="ltr" style="margin-right:0px;"> <div dir="ltr" style="margin-right:0px;"> <p dir="ltr" style="margin-right:0px;"></p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 493px; top: 1277px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 493px; top: 1277px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_17" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_17" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=17&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span>當使用</span><span class="keyword">group</span><span>&nbsp;</span><span class="keyword">by</span><span>子句時則</span><span class="keyword">select</span><span>中關於表的原始屬性名只能是被</span><span class="string">"by"</span><span>的屬性,如上例中的SNO。若想輸出其餘屬性列,則應在</span><span class="keyword">select</span><span>子句後加上相應的聚合函數(</span><span class="func">count</span><span>(*),</span><span class="func">count</span><span>(列名),</span><span class="func">sum</span><span>(列名),</span><span class="func">avg</span><span>(列名),</span><span class="keyword">max</span><span>(列名),</span><span class="keyword">min</span><span>(列名))。其實這也很容易理解,當參照SNO被分組後,每個SNO的具體實例與其餘任意一個屬性都變成一對一或一對多的關係,只有經過聚合函數將每組其餘想要的屬性的多個實例算成一個值,即SNO與其造成一對一的關係後才能列出。並且一旦SQL語句中使用了</span><span class="keyword">group</span><span>&nbsp;</span><span class="keyword">by</span><span>子句後聚合函數都變成分別對每組起做用。&nbsp;&nbsp;</span></span></li></ol></div><pre class="sql" name="code" style="display: none;"> 當使用group by子句時則select中關於表的原始屬性名只能是被"by"的屬性,如上例中的SNO。若想輸出其餘屬性列,則應在select子句後加上相應的聚合函數(count(*),count(列名),sum(列名),avg(列名),max(列名),min(列名))。其實這也很容易理解,當參照SNO被分組後,每個SNO的具體實例與其餘任意一個屬性都變成一對一或一對多的關係,只有經過聚合函數將每組其餘想要的屬性的多個實例算成一個值,即SNO與其造成一對一的關係後才能列出。並且一旦SQL語句中使用了group by子句後聚合函數都變成分別對每組起做用。</pre> </div> </blockquote> <div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 493px; top: 1452px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_9" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_9" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=9&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 493px; top: 1452px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_18" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_18" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=18&amp;width=16&amp;height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span>一條SQL語句個子句的前後做用過程:&nbsp;&nbsp;</span></span></li></ol></div><pre class="sql" name="code" style="display: none;">一條SQL語句個子句的前後做用過程:</pre> <h1 class="sql"><span style="font-size:13px;">&nbsp; from</span>→<span style="font-size:13px;">where</span><span style="font-size:24px;">→<span style="font-size:13px;">group by</span>→<span style="font-size:13px;">select(含聚合函數)</span></span><span style="font-size:24px;">→<span style="font-size:13px;">order by</span></span></h1> <p class="sql"><span style="font-size:24px;"><span style="font-size:13px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 因此當select子句中含有sum(grade) sum_grade時,最後的排序子句能夠是order by sum_grade desc。<br></span><br></span></p> </div>sql

相關文章
相關標籤/搜索