{"id":156647,"date":"2020-09-18T00:35:58","date_gmt":"2020-09-17T16:35:58","guid":{"rendered":"http:\/\/4563.org\/?p=156647"},"modified":"2020-09-18T00:35:58","modified_gmt":"2020-09-17T16:35:58","slug":"%e5%85%b3%e4%ba%8e-mysql-%e4%b8%ad-as-%e7%9a%84%e6%89%a7%e8%a1%8c%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=156647","title":{"rendered":"\u5173\u4e8e mysql \u4e2d as \u7684\u6267\u884c\u65f6\u95f4"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e mysql \u4e2d as \u7684\u6267\u884c\u65f6\u95f4               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nymrli <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u7f51\u4e0a\u4e86\u89e3\u5230 mysql \u7684\u6267\u884c\u987a\u5e8f\u5982\u4e0b\u6240\u793a:<\/p>\n<pre><code>(1) from  (3) join  (2) on  (4) where  (5) group by(\u5f00\u59cb\u4f7f\u7528 select \u4e2d\u7684\u522b\u540d\uff0c\u540e\u9762\u7684\u8bed\u53e5\u4e2d\u90fd\u53ef\u4ee5\u4f7f\u7528) (6) avg,sum....  (7) having  (8) select  (9) distinct  (10) order by  <\/code><\/pre>\n<p>\u90a3\u4e48\u5728\u4e0b\u9762\u7684 SQL \u8bed\u53e5\u4e2d\u4e3a\u4ec0\u4e48\u5148\u6267\u884c\u7684 Having \u4e2d\u80fd\u62ff\u5230\u4ece\u540e\u6267\u884c\u7684 select .. as t \u4e2d\u58f0\u660e\u7684 t \u53d8\u91cf\u5462?<\/p>\n<pre><code>SELECT emp_no, COUNT(emp_no) AS t FROM salaries GROUP BY emp_no HAVING t &gt; 15; <\/code><\/pre>\n<p>\u662f\u6211 SQL \u6267\u884c\u987a\u5e8f\u627e\u9519\u4e86\u5462, \u8fd8\u662f\u5728\u6267\u884c\u8fd9\u6761 SQL \u7684\u65f6\u5019\u4f1a\u628a AS \u7684\u7ed3\u679c\u90fd\u5148\u9884\u7f16\u8bd1? \u767e\u5ea6\u540e\u6ca1\u6709\u627e\u5230\u6709\u5173 AS \u5173\u952e\u5b57\u7684\u6267\u884c\u987a\u5e8f&#8230;<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3279239\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kiracyan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             having \u662f\u805a\u5408\u4e4b\u540e\u7684\u8fc7\u6ee4                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3279240\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nbstoner <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u8fd8\u662f having \u5bf9\u805a\u5408\u7684\u8fc7\u6ee4\u5462\u3002as \u53ea\u662f\u7ed9\u4e2a\u522b\u540d\u800c\u5df2                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3279241\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : offswitch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             A MySQL extension to standard SQL permits references in the HAVING clause to aliased expressions in the select list. The HAVING clause can refer to aliases regardless of whether ONLY_FULL_GROUP_BY is enabled.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3279242\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhangysh1995 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6267\u884c\u987a\u5e8f\u4f1a\u88ab\u4f18\u5316\uff0c\u4e0d\u4e00\u5b9a\u662f\u4f60\u5217\u51fa\u6765\u7684\u8fd9\u4e2a\u987a\u5e8f\u3002\u5bf9\u4e8e\u7ed9\u51fa\u7684\u8fd9\u6761\u6267\u884c\u987a\u5e8f\u5982\u4e0b\uff1a<\/p>\n<p>1. group by<br \/>2. count<br \/>3. having<\/p>\n<p>MySQL \u5728\u5904\u7406\u8fd9\u6761\u8bed\u53e5\u7684\u65f6\u5019\uff0c\u53d1\u73b0 having \u4f7f\u7528\u4e86 t\uff0c\u4f1a\u5728\u8fd9\u6761\u8bed\u53e5\u7684\u7b26\u53f7\u91cc\u9762\u627e t \u7684\u5b9a\u4e49\uff0c\u8fd9\u662f parser \u9636\u6bb5\u505a\u7684\u4e8b\u60c5\uff0c\u4e0d\u662f\u52a8\u6001\u5b9e\u9645\u6267\u884c\u7684\u65f6\u5019\u7684\u4e8b\u60c5\u3002\u4f60\u53ef\u4ee5 explain\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u5982\u4f55 resolve \u4e86\u8fd9\u4e2a\u7b26\u53f7\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3279243\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : nymrli <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zhangysh1995 \u8c22\u8c22, \u6211\u4e4b\u524d\u6709\u7528 exlpain \u8bd5\u8fc7, \u4f46\u662f\u8c8c\u4f3c\u770b\u4e0d\u4e86\u8fd9\u4e2a\u5427? (\u5e94\u8be5\u662f\u6211\u80fd\u529b\u6709\u9650)<br \/>&#8220;`sql<br \/>use niuke;<\/p>\n<p>CREATE TABLE `salaries` (<br \/>`emp_no` int(11) NOT NULL,<br \/>`salary` int(11) NOT NULL,<br \/>`from_date` date NOT NULL,<br \/>`to_date` date NOT NULL,<br \/>PRIMARY KEY (`emp_no`,`from_date`));<\/p>\n<p>explain select s.emp_no, count(s.emp_no) as t<br \/>from salaries s<br \/>group by s.emp_no<br \/>having t &gt; 15;<br \/>&#8220;`<br \/>SQL \u5982\u4e0a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3279244\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : nymrli <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @offswitch Nice, \u8c22\u8c22\u5927\u4f6c\uff0c \u53ea\u4e0d\u8fc7\u6211\u6709\u70b9\u597d\u5947\u4ed6\u662f\u600e\u4e48\u5b9e\u73b0\u8fd9\u4e2a\u5728 group by \u7684\u65f6\u5019\u62ff\u5230 select \u4e2d\u7684\u522b\u540d\u7684\u3002why and how can the HAVING clause refer to aliases\uff0c \u5373\u662f\u5426 SQL \u4f1a\u8fdb\u884c\u4f18\u5316\uff0c\u53ca\u5982\u4f55\u5b9e\u73b0\u4f18\u5316 SQL \u8bed\u53e5\u7684\u6267\u884c\u987a\u5e8f\u7684\u3001                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3279245\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chihiro2014 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nymrli \u4f60\u5f97\u53bb\u770b\u67e5\u8be2\u4f18\u5316\u5668\u8fd9\u5757\uff0c\u4e0d\u540c\u7684 DBMS \u8fd9\u5757\u7684\u505a\u6cd5\u4e5f\u4e0d\u540c\u3002\u56e0\u4e3a\u6bcf\u5bb6\u6570\u636e\u5e93\u5382\u5546\u5bf9\u4e8e SQL \u7684\u5b9e\u73b0\u90fd\u662f\u5343\u5dee\u4e07\u522b\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e mysql \u4e2d as \u7684\u6267\u884c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/156647"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=156647"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/156647\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156647"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}