{"id":70,"date":"2026-08-30T21:04:55","date_gmt":"2026-08-30T13:04:55","guid":{"rendered":"https:\/\/www.233xianyu.top\/?p=70"},"modified":"2026-08-30T21:05:33","modified_gmt":"2026-08-30T13:05:33","slug":"4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/","title":{"rendered":"04\u3001\u6570\u636e\u5e93\u7684\u67e5\u8be2"},"content":{"rendered":"<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>group by\u4f8b\u5b50\r\n\r\nselect post,avg(salary) from employee group by post;\r\n\r\n\u53d6\u51fa\u6bcf\u4e2a\u90e8\u95e8\u7684\u5458\u5de5\u6570\r\nselect post,count(id) from employee group by post;\r\n\r\n\u6c42\u7537\u4eba\u6570\u4e0e\u5973\u4eba\u6570\r\nselect sex,count(id) from employee group by sex;\r\n\r\n\u6c42\u5e74\u9f84\u572820\u5c81\u4ee5\u4e0a\u7684\u7537\u4eba\u6570\u4e0e\u5973\u4eba\u6570\r\nselect sex,count(id) from employee where age &gt; 20 group by sex;\r\n\r\n\u6c42\u6bcf\u4e2a\u90e8\u95e820\u5c81\u4ee5\u4e0a\u7684\u4eba\u7684\u5e73\u5747\u85aa\u8d44\r\nselect post,avg(salary) from employee where age &gt; 20 group by post;\r\n\r\n\r\n----------------------------------------\r\nhaving\u4f8b\u5b50\r\n\r\n\u67e5\u51fa\u5e73\u5747\u85aa\u8d44\u572810000\u4ee5\u4e0a\u7684\u90e8\u95e8\r\nselect post,avg(salary) from employee group by post having avg(salary) &gt; 10000;\r\n\r\n\u67e5\u51fa\u90e8\u95e8\u5185\u7537\u5458\u5de5\u5e73\u5747\u5de5\u8d44\u57283000\u4ee5\u4e0a\u7684\u90e8\u95e8\r\nselect post,avg(salary) from employee where sex=\"male\" group by post having avg(salary) &gt; 3000;\r\n\r\n\r\n-----------------------------------------------\r\norder by\r\n\r\nselect * from employee order by age asc;\r\nselect * from employee order by age desc;\r\n\r\n\u67e5\u51fa\u90e8\u95e8\u5185\u7537\u5458\u5de5\u5e73\u5747\u5de5\u8d44\u57283000\u4ee5\u4e0a\u7684\u90e8\u95e8\u4e14\u4ee5\u8fd9\u4e2a\u5e73\u5747\u5de5\u8d44\u6392\u5e8f(\u5347\u5e8f)\r\nselect post,avg(salary) from employee where sex=\"male\" group by post having avg(salary) &gt; 3000 order by avg(salary);\r\n\r\n\r\n----------------------------------------\r\nlimit\r\n\r\n\u53d6\u51fa\u85aa\u8d44\u6700\u9ad8\u7684\u90a3\u4e2a\u4eba\u7684\u4fe1\u606f\r\nselect * from employee order by salary desc limit 1;\r\n\r\n\u4ece0\u5f00\u59cb\u5f80\u540e\u8bfb5\u6761,\u4ee5\u6b64\u7c7b\u63a8\u8bfb\u5230\u7b2c20\u6761\r\nselect * from employee limit 0,5;\r\nselect * from employee limit 5,5;\r\nselect * from employee limit 10,5;\r\nselect * from employee limit 15,5;\r\n\r\n\r\n-----------------------------------\r\n\u591a\u8868\u67e5\u8be2\r\n\u5185\u8fde\u63a5\r\n\r\n\u67e5\u8be2\u6280\u672f\u90e8\u7684\u5458\u5de5\u7684\u59d3\u540d\r\nselect employee.name from employee inner join department on employee.dep_id = department.id where department.name = \"\u6280\u672f\";\r\n\r\n\u67e5\u8be2\u5e73\u5747\u5e74\u9f84\u5927\u4e8e25\u5c81\u7684\u90e8\u95e8\u540d\r\nselect department.name,avg(age) from employee inner join department on employee.dep_id = department.id\r\ngroup by department.name having avg(age) &gt; 25;\r\n\r\n\r\n-----------------------------------------------------\r\n\u5b50\u67e5\u8be2\r\n\r\n\u67e5\u8be2\u51fa\u90a3\u4e9b\u85aa\u8d44\u6bd4\u6240\u6709\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\u90fd\u9ad8\u7684\u5458\u5de5=\u300b\u85aa\u8d44\u5728\u6240\u6709\u90e8\u95e8\u5e73\u5747\u7ebf\u4ee5\u4e0a\u7684\u72d7\u5e01\u8d44\u672c\u5bb6\r\nselect * from employee where salary &gt; all\r\n(select avg(salary) from employee group by post);\r\n\r\n\u67e5\u8be2\u51fa\u90a3\u4e9b\u85aa\u8d44\u6bd4\u6240\u6709\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\u90fd\u4f4e\u7684\u5458\u5de5=\u300b\u85aa\u8d44\u5728\u6240\u6709\u90e8\u95e8\u5e73\u5747\u7ebf\u4ee5\u4e0b\u7684\u65e0\u4ea7\u9636\u7ea7\u52b3\u82e6\u5927\u4f17\r\nselect * from employee where salary &lt; all\r\n(select avg(salary) from employee group by post);\r\n\r\n\u67e5\u8be2\u51fa\u90a3\u4e9b\u85aa\u8d44\u6bd4\u4efb\u610f\u4e00\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\u9ad8\u7684\u5458\u5de5=\u300b\u85aa\u8d44\u5728\u4efb\u4e00\u90e8\u95e8\u5e73\u5747\u7ebf\u4ee5\u4e0a\u7684\u5458\u5de5\r\nselect * from employee where salary &gt; all\r\n(select avg(salary) from employee group by post);\r\n<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":2} --><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_87 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#%E4%B8%80_%E5%88%86%E7%BB%84%E6%9F%A5%E8%AF%A2%EF%BC%9Agroup_by\" >\u4e00 \u5206\u7ec4\u67e5\u8be2\uff1agroup by<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#11_%E6%9F%A5%E8%AF%A2%E9%A1%BA%E5%BA%8F\" >1.1 \u67e5\u8be2\u987a\u5e8f<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#1%E3%80%81%E5%8D%95%E8%A1%A8%E6%9F%A5%E8%AF%A2\" >1\u3001\u5355\u8868\u67e5\u8be2<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#2%E3%80%81%E5%B8%A6IN%E5%85%B3%E9%94%AE%E5%AD%97%E7%9A%84%E5%AD%90%E6%9F%A5%E8%AF%A2\" >2\u3001\u5e26IN\u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#12_%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0\" >1.2 \u805a\u5408\u51fd\u6570<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#%E4%BA%8C_having%E8%BF%87%E6%BB%A4%EF%BC%9A%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0\" >\u4e8c having\u8fc7\u6ee4\uff1a\u53ef\u4ee5\u4f7f\u7528\u805a\u5408\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#%E4%B8%89_order_by%E4%B8%8Elimit\" >\u4e09 order by\u4e0elimit<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#31_asc%E5%92%8Cdesc\" >3.1 asc\u548cdesc<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#32_%E5%A6%82%E4%BD%95%E5%9C%A8%E5%90%8C%E4%B8%80%E4%B8%AA%E7%BA%A7%E5%88%AB%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%E6%8E%92%E5%BA%8F\" >3.2 \u5982\u4f55\u5728\u540c\u4e00\u4e2a\u7ea7\u522b\u7684\u60c5\u51b5\u4e0b\u6392\u5e8f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#33_limit%E6%8E%A7%E5%88%B6%E6%98%BE%E7%A4%BA%E7%9A%84%E6%9D%A1%E6%95%B0\" >3.3 limit\u63a7\u5236\u663e\u793a\u7684\u6761\u6570<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#%E5%9B%9B_%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2\" >\u56db \u591a\u8868\u67e5\u8be2<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#41_%E5%86%85%E8%BF%9E%E6%8E%A5%EF%BC%9A%E5%8F%AA%E4%BF%9D%E7%95%99%E6%9C%89%E5%AF%B9%E5%BA%94%E5%85%B3%E7%B3%BB%E7%9A%84%E8%AE%B0%E5%BD%95\" >4.1 \u5185\u8fde\u63a5\uff1a\u53ea\u4fdd\u7559\u6709\u5bf9\u5e94\u5173\u7cfb\u7684\u8bb0\u5f55<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#42_%E5%B7%A6%E8%BF%9E%E6%8E%A5%EF%BC%9A%E5%9C%A8%E5%86%85%E8%BF%9E%E6%8E%A5%E7%9A%84%E5%9F%BA%E7%A1%80%E4%B9%8B%E4%B8%8A%E5%B0%86%E5%B7%A6%E8%BE%B9%E6%B2%A1%E6%9C%89%E5%AF%B9%E5%BA%94%E5%85%B3%E7%B3%BB%E7%9A%84%E8%AE%B0%E5%BD%95%E4%B9%9F%E4%BF%9D%E7%95%99%E4%BA%86\" >4.2 \u5de6\u8fde\u63a5\uff1a\u5728\u5185\u8fde\u63a5\u7684\u57fa\u7840\u4e4b\u4e0a\u5c06\u5de6\u8fb9\u6ca1\u6709\u5bf9\u5e94\u5173\u7cfb\u7684\u8bb0\u5f55\u4e5f\u4fdd\u7559\u4e86<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#43_%E5%8F%B3%E8%BF%9E%E6%8E%A5%EF%BC%9A%E5%9C%A8%E5%86%85%E8%BF%9E%E6%8E%A5%E7%9A%84%E5%9F%BA%E7%A1%80%E4%B9%8B%E4%B8%8A%E5%B0%86%E5%8F%B3%E8%BE%B9%E6%B2%A1%E6%9C%89%E5%AF%B9%E5%BA%94%E5%85%B3%E7%B3%BB%E7%9A%84%E8%AE%B0%E5%BD%95%E4%B9%9F%E4%BF%9D%E7%95%99%E4%BA%86\" >4.3 \u53f3\u8fde\u63a5\uff1a\u5728\u5185\u8fde\u63a5\u7684\u57fa\u7840\u4e4b\u4e0a\u5c06\u53f3\u8fb9\u6ca1\u6709\u5bf9\u5e94\u5173\u7cfb\u7684\u8bb0\u5f55\u4e5f\u4fdd\u7559\u4e86<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#44_%E5%85%A8%E8%BF%9E%E6%8E%A5%EF%BC%9Afull_join\" >4.4 \u5168\u8fde\u63a5\uff1afull join<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#%E4%BA%94_%E7%89%A9%E7%90%86%E8%A1%A8%E4%B8%8E%E8%99%9A%E6%8B%9F%E8%A1%A8%E7%9A%84%E8%BF%9E%E6%8E%A5\" >\u4e94 \u7269\u7406\u8868\u4e0e\u865a\u62df\u8868\u7684\u8fde\u63a5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#%E5%85%AD_%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%9A%84%E7%94%A8%E6%B3%95\" >\u516d \u5b50\u67e5\u8be2\u7684\u7528\u6cd5<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.233xianyu.top\/index.php\/2026\/08\/30\/4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%9f%a5%e8%af%a2\/#61_%E5%85%B3%E4%BA%8E%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%9A%84%E5%85%B3%E9%94%AE%E5%AD%97\" >6.1 \u5173\u4e8e\u5b50\u67e5\u8be2\u7684\u5173\u952e\u5b57<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%E4%B8%80_%E5%88%86%E7%BB%84%E6%9F%A5%E8%AF%A2%EF%BC%9Agroup_by\"><\/span>\u4e00 \u5206\u7ec4\u67e5\u8be2\uff1agroup by<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"11_%E6%9F%A5%E8%AF%A2%E9%A1%BA%E5%BA%8F\"><\/span>1.1 \u67e5\u8be2\u987a\u5e8f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:heading {\"level\":4} --><\/p>\n<h4><span class=\"ez-toc-section\" id=\"1%E3%80%81%E5%8D%95%E8%A1%A8%E6%9F%A5%E8%AF%A2\"><\/span>1\u3001\u5355\u8868\u67e5\u8be2<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>select distinct \u5b57\u6bb51\uff0c\u5b57\u6bb52\uff0c\u3002\u3002\u3002 from \u5e93.\u8868\r\n    where \u8fc7\u6ee4\u6761\u4ef6\r\n    group by \u5206\u7ec4\u5b57\u6bb5\r\n    having \u8fc7\u6ee4\u6761\u4ef6\r\n    order by \u6392\u5e8f\u5b57\u6bb5\r\n    limit \u6761\u6570;\r\n\u67e5\u8be2\u987a\u5e8f\uff1afrom\u5e93.\u8868-----&gt;where \u8fc7\u6ee4\u6761\u4ef6-----&gt;group by \u5206\u7ec4\u5b57\u6bb5-----&gt;having \u8fc7\u6ee4\u6761\u4ef6-----&gt;distinct \u5b57\u6bb51\uff0c\u5b57\u6bb52\uff0c\u3002\u3002\u3002  <\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":4} --><\/p>\n<h4><span class=\"ez-toc-section\" id=\"2%E3%80%81%E5%B8%A6IN%E5%85%B3%E9%94%AE%E5%AD%97%E7%9A%84%E5%AD%90%E6%9F%A5%E8%AF%A2\"><\/span>2\u3001\u5e26IN\u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"12_%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0\"><\/span>1.2 \u805a\u5408\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:list --><\/p>\n<ul>\n<li>avg\u5e73\u5747<\/li>\n<li>max\u6700\u5927\u503c<\/li>\n<li>min\u6700\u5c0f\u503c<\/li>\n<li>sum\u548c<\/li>\n<li>count\u603b\u6570<\/li>\n<li>group_concat<\/li>\n<\/ul>\n<p><!-- \/wp:list --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u5206\u7ec4\u4e4b\u540e\uff1aselect\u53ea\u80fd\u770b\u5230\u5206\u7ec4\u5b57\u6bb5\u4ee5\u53ca\u805a\u5408\u7684\u7ed3\u679c<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u6211\u4eec\u5199\u51fa\u6709\u6b67\u4e49\u3001\u53ef\u80fd\u4ea7\u751f\u4e0d\u53ef\u9884\u77e5\u7ed3\u679c\u7684GROUP BY\u67e5\u8be2\uff0c\u9700\u8981\u5f00\u542fONLY_FULL_GROUP_BY\u8fd9\u4e2a\u5b89\u5168\u6a21\u5f0f<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>1\u3001\u67e5\u770b\u662f\u5426\u5f00\u542fONLY_FULL_GROUP_BY\u6a21\u5757\r\nSELECT @@sql_mode;\r\n\u5982\u679c\u8fd4\u56de\u7ed3\u679c\u91cc\u6ca1\u6709 ONLY_FULL_GROUP_BY\uff0c\u8bf4\u660e\u8fd9\u4e2a\u529f\u80fd\u662f\u5173\u95ed\u7684\u3002\r\n\r\n2\u3001\u5168\u5c40\u5f00\u542f\uff0c\u6570\u636e\u5e93\u91cd\u542f\u5931\u6548\r\nSET GLOBAL sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';\r\n\r\n3\u3001\u6c38\u4e45\u5f00\u542f(\u4fee\u6539\u914d\u7f6e\u6587\u4ef6)\r\n\r\nvim \/data\/3307\/my.cnf\r\n\r\n----------------------------\r\n[mysqld]\r\n\r\nsql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'\r\n\r\n----------------------------<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":2} --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E4%BA%8C_having%E8%BF%87%E6%BB%A4%EF%BC%9A%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0\"><\/span>\u4e8c having\u8fc7\u6ee4\uff1a\u53ef\u4ee5\u4f7f\u7528\u805a\u5408\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>select distinct \u5b57\u6bb51\uff0c\u5b57\u6bb52\uff0c\u3002\u3002\u3002 from \u5e93.\u8868\r\n    where \u8fc7\u6ee4\u6761\u4ef6\r\n    group by \u5206\u7ec4\u5b57\u6bb5\r\n    having \u8fc7\u6ee4\u6761\u4ef6<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":2} --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E4%B8%89_order_by%E4%B8%8Elimit\"><\/span>\u4e09 order by\u4e0elimit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"31_asc%E5%92%8Cdesc\"><\/span>3.1 asc\u548cdesc<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u5728\u4f7f\u7528order by\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c\u9ed8\u8ba4\u662fasc&#8211;&gt;\u4ece\u5c0f\u5230\u5927<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>desc\u5219\u662f\u4ece\u5927\u5230\u5c0f<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"32_%E5%A6%82%E4%BD%95%E5%9C%A8%E5%90%8C%E4%B8%80%E4%B8%AA%E7%BA%A7%E5%88%AB%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%E6%8E%92%E5%BA%8F\"><\/span>3.2 \u5982\u4f55\u5728\u540c\u4e00\u4e2a\u7ea7\u522b\u7684\u60c5\u51b5\u4e0b\u6392\u5e8f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code># \u5728\u8fd9\u4e2a\u547d\u4ee4\u91cc\uff0c\u5982\u679cage\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\uff0c\u4f1a\u518d\u6839\u636eid\u6765\u8fdb\u884c\u964d\u5e8f\u6392\u5217\r\nselect * from employee order by age asc,id desc;<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"33_limit%E6%8E%A7%E5%88%B6%E6%98%BE%E7%A4%BA%E7%9A%84%E6%9D%A1%E6%95%B0\"><\/span>3.3 limit\u63a7\u5236\u663e\u793a\u7684\u6761\u6570<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:heading {\"level\":2} --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%9B%9B_%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2\"><\/span>\u56db \u591a\u8868\u67e5\u8be2<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u7b1b\u5361\u5c14\u4f53\u79ef\uff1a\u5de6\u8fb9\u4e00\u4e2a\u8bb0\u5f55\u6bd4\u5bf9\u53f3\u8fb9\u5168\u8868\uff0c\u4f9d\u6b21\u6bd4\u5bf9<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u591a\u8868\u67e5\u8be2\u662f\u5c06\u4e24\u4e2a\u8868\u62fc\u8d77\u6765\u5e76\u67e5\u8be2<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>select * from employee,department where employee.dep_id = department.id;<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"41_%E5%86%85%E8%BF%9E%E6%8E%A5%EF%BC%9A%E5%8F%AA%E4%BF%9D%E7%95%99%E6%9C%89%E5%AF%B9%E5%BA%94%E5%85%B3%E7%B3%BB%E7%9A%84%E8%AE%B0%E5%BD%95\"><\/span>4.1 \u5185\u8fde\u63a5\uff1a\u53ea\u4fdd\u7559\u6709\u5bf9\u5e94\u5173\u7cfb\u7684\u8bb0\u5f55<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>select * from employee inner join department on employee.dep_id = department.id;\r\n\r\n#\u5728\u8868\u591a\u5bf9\u591a\u7684\u60c5\u51b5\u4e0b\r\nselect * from author inner join author2book on author.id = author2book.author_id inner join book on author2book_id = book.id;<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"42_%E5%B7%A6%E8%BF%9E%E6%8E%A5%EF%BC%9A%E5%9C%A8%E5%86%85%E8%BF%9E%E6%8E%A5%E7%9A%84%E5%9F%BA%E7%A1%80%E4%B9%8B%E4%B8%8A%E5%B0%86%E5%B7%A6%E8%BE%B9%E6%B2%A1%E6%9C%89%E5%AF%B9%E5%BA%94%E5%85%B3%E7%B3%BB%E7%9A%84%E8%AE%B0%E5%BD%95%E4%B9%9F%E4%BF%9D%E7%95%99%E4%BA%86\"><\/span>4.2 \u5de6\u8fde\u63a5\uff1a\u5728\u5185\u8fde\u63a5\u7684\u57fa\u7840\u4e4b\u4e0a\u5c06\u5de6\u8fb9\u6ca1\u6709\u5bf9\u5e94\u5173\u7cfb\u7684\u8bb0\u5f55\u4e5f\u4fdd\u7559\u4e86<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>select * from employee left join department on employee.dep_id = department.id;<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"43_%E5%8F%B3%E8%BF%9E%E6%8E%A5%EF%BC%9A%E5%9C%A8%E5%86%85%E8%BF%9E%E6%8E%A5%E7%9A%84%E5%9F%BA%E7%A1%80%E4%B9%8B%E4%B8%8A%E5%B0%86%E5%8F%B3%E8%BE%B9%E6%B2%A1%E6%9C%89%E5%AF%B9%E5%BA%94%E5%85%B3%E7%B3%BB%E7%9A%84%E8%AE%B0%E5%BD%95%E4%B9%9F%E4%BF%9D%E7%95%99%E4%BA%86\"><\/span>4.3 \u53f3\u8fde\u63a5\uff1a\u5728\u5185\u8fde\u63a5\u7684\u57fa\u7840\u4e4b\u4e0a\u5c06\u53f3\u8fb9\u6ca1\u6709\u5bf9\u5e94\u5173\u7cfb\u7684\u8bb0\u5f55\u4e5f\u4fdd\u7559\u4e86<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>select * from employee right join department on employee.dep_id = department.id;<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"44_%E5%85%A8%E8%BF%9E%E6%8E%A5%EF%BC%9Afull_join\"><\/span>4.4 \u5168\u8fde\u63a5\uff1afull join<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>select * from employee left join department on employee.dep_id = department.id\r\nunion\r\nselect * from employee right join department on employee.dep_id = department.id;<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":2} --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E4%BA%94_%E7%89%A9%E7%90%86%E8%A1%A8%E4%B8%8E%E8%99%9A%E6%8B%9F%E8%A1%A8%E7%9A%84%E8%BF%9E%E6%8E%A5\"><\/span>\u4e94 \u7269\u7406\u8868\u4e0e\u865a\u62df\u8868\u7684\u8fde\u63a5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code>\u5982\u4f55\u5c06\u865a\u62df\u8868\u53d8\u4e3a\u7269\u7406\u8868\u7684\u5f62\u5f0f\uff1f\r\n(select id,name from employee) as t1; \r\n\r\n# \u67e5\u8be2\u6bcf\u4e2a\u90e8\u95e8\u6700\u65b0\u5165\u804c\u7684\u90a3\u4e9b\u5458\u5de5\r\nselect employee.name,employee.hire_date,employee.post,t1.post,t1.m_d from employee inner join (select post,max(hire_date) as m_d from employee group by post) as t1 on employee.post = t1.post where employee.hire_date = t1.m_d;<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":2} --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%85%AD_%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%9A%84%E7%94%A8%E6%B3%95\"><\/span>\u516d \u5b50\u67e5\u8be2\u7684\u7528\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:code --><\/p>\n<pre class=\"wp-block-code\"><code># \u5148\u4ece\u4e00\u5f20\u8868\u4e2d\u67e5\u51fa\u7ed3\u679c\uff0c\u7136\u540e\u4ee5\u8be5\u7ed3\u679c\u4f5c\u4e3a\u6761\u4ef6\u53bb\u67e5\u4e0b\u4e00\u5f20\r\nselect * from employee where dep_id = \r\n(select id from department where name = \"\u6280\u672f\");\r\n\r\n#\u67e5\u8be2\u5e73\u5747\u5e74\u9f84\u572825\u5c81\u4ee5\u4e0a\u7684\u90e8\u95e8\u540d\r\nselect name from department where id in\r\n(select dep_id from employee group by dep_id having avg(age) &gt; 25);\r\n\r\n#\u67e5\u770b\u4e0d\u8db31\u4eba\u7684\u90e8\u95e8\u540d(\u5b50\u67e5\u8be2\u5f97\u5230\u7684\u662f\u6709\u4eba\u90e8\u95e8\u7684id)\r\nselect * from department where id not in\r\n(select distinct dep_id from employee);\r\n\r\n# \u67e5\u8be2\u5927\u4e8e\u6240\u6709\u4eba\u5e73\u5747\u5e74\u9f84\u7684\u5458\u5de5\u540d\u4e0e\u5e74\u9f84\r\nselect * from employee where age &gt;\r\n(select avg(age) from employee);<\/code><\/pre>\n<p><!-- \/wp:code --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3><span class=\"ez-toc-section\" id=\"61_%E5%85%B3%E4%BA%8E%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%9A%84%E5%85%B3%E9%94%AE%E5%AD%97\"><\/span>6.1 \u5173\u4e8e\u5b50\u67e5\u8be2\u7684\u5173\u952e\u5b57<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>1\u3001\u5e26in\u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2\uff0cin\u540e\u9762\u7684\u6761\u4ef6\u662f\u7b49\u4e8e\u6761\u4ef6\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5373\u53ef<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>2\u3001\u5e26any\u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2\uff0c\u901a\u5e38\u7528\u4e8e\u6bd4\u8f83\uff0c\u80fd\u6ee1\u8db3\u6bd4\u8f83any\u540e\u9762\u7684\u6761\u4ef6\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u6bb5\u5373\u53ef<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>3\u3001\u5e26all\u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2\uff0c\u901a\u5e38\u7528\u4e8e\u6bd4\u8f83\uff0c\u80fd\u6ee1\u8db3\u6bd4\u8f83all\u540e\u9762\u7684\u6761\u4ef6\u4e2d\u7684\u6240\u6709\u5b57\u6bb5<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u6ce8\u610f\uff1aany\u548call\u53ea\u80fd\u7528\u5728\u5b50\u67e5\u8be2\u4e4b\u4e2d<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00 \u5206\u7ec4\u67e5\u8be2\uff1agroup by 1.1 \u67e5\u8be2\u987a\u5e8f 1\u3001\u5355\u8868\u67e5\u8be2 2\u3001\u5e26IN\u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2 1.2 \u805a\u5408\u51fd\u6570 avg\u5e73\u5747 max\u6700\u5927\u503c min\u6700\u5c0f\u503c sum\u548c count\u603b\u6570 group_concat \u5206\u7ec4\u4e4b\u540e\uff1aselect\u53ea\u80fd\u770b\u5230\u5206\u7ec4\u5b57\u6bb5\u4ee5\u53ca\u805a\u5408\u7684\u7ed3\u679c \u4e3a\u4e86\u907f\u514d\u6211\u4eec\u5199\u51fa\u6709\u6b67\u4e49\u3001\u53ef\u80fd\u4ea7\u751f\u4e0d\u53ef\u9884\u77e5\u7ed3\u679c\u7684GROUP BY\u67e5\u8be2\uff0c\u9700\u8981\u5f00\u542fONLY_FULL_GROUP_BY\u8fd9\u4e2a\u5b89\u5168\u6a21\u5f0f \u4e8c having\u8fc7\u6ee4 [&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":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":2,"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions\/73"}],"wp:attachment":[{"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.233xianyu.top\/index.php\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}