{"id":153,"date":"2013-04-10T20:13:38","date_gmt":"2013-04-10T12:13:38","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=153"},"modified":"2013-04-10T20:13:38","modified_gmt":"2013-04-10T12:13:38","slug":"sql-having-%e5%ad%90%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=153","title":{"rendered":"SQL HAVING \u5b50\u53e5"},"content":{"rendered":"<div>\n<h2>HAVING \u5b50\u53e5<\/h2>\n<p>\u5728 SQL \u4e2d\u589e\u52a0 HAVING \u5b50\u53e5\u539f\u56e0\u662f\uff0cWHERE \u5173\u952e\u5b57\u65e0\u6cd5\u4e0e\u5408\u8ba1\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<h3>SQL HAVING \u8bed\u6cd5<\/h3>\n<pre>SELECT column_name, aggregate_function(column_name)\nFROM table_name\nWHERE column_name operator value\nGROUP BY column_name\nHAVING aggregate_function(column_name) operator value<\/pre>\n<\/div>\n<div>\n<h2>SQL HAVING \u5b9e\u4f8b<\/h2>\n<p>\u6211\u4eec\u62e5\u6709\u4e0b\u9762\u8fd9\u4e2a &#8220;Orders&#8221; \u8868\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<th>O_Id<\/th>\n<th>OrderDate<\/th>\n<th>OrderPrice<\/th>\n<th>Customer<\/th>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2008\/12\/29<\/td>\n<td>1000<\/td>\n<td>Bush<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2008\/11\/23<\/td>\n<td>1600<\/td>\n<td>Carter<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2008\/10\/05<\/td>\n<td>700<\/td>\n<td>Bush<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>2008\/09\/28<\/td>\n<td>300<\/td>\n<td>Bush<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2008\/08\/06<\/td>\n<td>2000<\/td>\n<td>Adams<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>2008\/07\/21<\/td>\n<td>100<\/td>\n<td>Carter<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5e0c\u671b\u67e5\u627e\u8ba2\u5355\u603b\u91d1\u989d\u5c11\u4e8e 2000 \u7684\u5ba2\u6237\u3002<br \/>\n\u6211\u4eec\u4f7f\u7528\u5982\u4e0b SQL \u8bed\u53e5\uff1a<\/p>\n<pre>SELECT Customer,SUM(OrderPrice) FROM Orders\nGROUP BY Customer\nHAVING SUM(OrderPrice)&lt;2000<\/pre>\n<p>\u7ed3\u679c\u96c6\u7c7b\u4f3c\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<th>Customer<\/th>\n<th>SUM(OrderPrice)<\/th>\n<\/tr>\n<tr>\n<td>Carter<\/td>\n<td>1700<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u73b0\u5728\u6211\u4eec\u5e0c\u671b\u67e5\u627e\u5ba2\u6237 &#8220;Bush&#8221; \u6216 &#8220;Adams&#8221; \u62e5\u6709\u8d85\u8fc7 1500 \u7684\u8ba2\u5355\u603b\u91d1\u989d\u3002<br \/>\n\u6211\u4eec\u5728 SQL \u8bed\u53e5\u4e2d\u589e\u52a0\u4e86\u4e00\u4e2a\u666e\u901a\u7684 WHERE \u5b50\u53e5\uff1a<\/p>\n<pre>SELECT Customer,SUM(OrderPrice) FROM Orders\nWHERE Customer='Bush' OR Customer='Adams'\nGROUP BY Customer\nHAVING SUM(OrderPrice)&gt;1500<\/pre>\n<p>\u7ed3\u679c\u96c6\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<th>Customer<\/th>\n<th>SUM(OrderPrice)<\/th>\n<\/tr>\n<tr>\n<td>Bush<\/td>\n<td>2000<\/td>\n<\/tr>\n<tr>\n<td>Adams<\/td>\n<td>2000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<br \/>\n<a title=\"http:\/\/www.w3school.com.cn\/sql\/sql_having.asp\" href=\"http:\/\/www.w3school.com.cn\/sql\/sql_having.asp\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.w3school.com.cn\/sql\/sql_having.asp<\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>HAVING \u5b50\u53e5 \u5728 SQL \u4e2d\u589e\u52a0 HAVING \u5b50\u53e5\u539f\u56e0\u662f\uff0cWHERE \u5173\u952e\u5b57\u65e0\u6cd5\u4e0e\u5408\u8ba1\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002 SQL HAVING \u8bed\u6cd5 SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value SQL HAVING \u5b9e\u4f8b \u6211\u4eec\u62e5\u6709\u4e0b\u9762\u8fd9\u4e2a &#8220;Orders&#8221; \u8868\uff1a O_Id OrderDate OrderPrice Customer 1 2008\/12\/29 1000 Bush 2 2008\/11\/23 1600 Carter 3 2008\/10\/05 700 Bush 4 2008\/09\/28 300 Bush 5 2008\/08\/06 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[102],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-study","tag-sql"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}