{"id":3551,"date":"2018-07-16T10:07:35","date_gmt":"2018-07-16T02:07:35","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=3551"},"modified":"2018-07-19T13:25:50","modified_gmt":"2018-07-19T05:25:50","slug":"%e5%8d%87%e7%ba%a7mysql5-7%ef%bc%8c%e5%bc%80%e5%8f%91%e4%b8%8d%e5%be%97%e4%b8%8d%e6%b3%a8%e6%84%8f%e7%9a%84%e5%9d%91","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/jishu\/shujuku\/%e5%8d%87%e7%ba%a7mysql5-7%ef%bc%8c%e5%bc%80%e5%8f%91%e4%b8%8d%e5%be%97%e4%b8%8d%e6%b3%a8%e6%84%8f%e7%9a%84%e5%9d%91\/","title":{"rendered":"\u5347\u7ea7MySQL5.7\uff0c\u5f00\u53d1\u4e0d\u5f97\u4e0d\u6ce8\u610f\u7684\u5751"},"content":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\uff0c\u5c06\u7ebf\u4e0aMySQL\u6570\u636e\u5e93\u5347\u7ea7\u5230\u4e865.7\u3002\u8003\u8651\u5230\u53ef\u80fd\u4ea7\u751f\u7684\u4e0d\u517c\u5bb9\u6027\uff0c\u5728\u5347\u7ea7\u4e4b\u524d\uff0c\u786e\u5b9e\u4e5f\u662f\u6218\u6218\u5162\u5162\uff0c\u867d\u7136\u6d4b\u8bd5\u73af\u5883\uff0c\u5f00\u53d1\u73af\u5883\u65e9\u5728\u534a\u5e74\u524d\u5c31\u5df2\u63d0\u524d\u5347\u7ea7\u3002<\/p>\n<p>\u57fa\u4e8e\u524d\u671f\u7684\u8c03\u7814\u548c\u670b\u53cb\u7684\u53cd\u9988\uff0c\u4e0e\u5f00\u53d1\u76f8\u5173\u7684\u4e3b\u8981\u6709\u4e24\u70b9\uff1a<\/p>\n<p>sql_mode<\/p>\n<p>MySQL 5.6\u4e2d\uff0c\u5176\u9ed8\u8ba4\u503c\u4e3a&#8221;NO_ENGINE_SU BSTITUTION&#8221;\uff0c\u53ef\u7406\u89e3\u4e3a\u975e\u4e25\u683c\u6a21\u5f0f\uff0c\u8b6c\u5982\uff0c\u5bf9\u81ea\u589e\u4e3b\u952e\u63d2\u5165\u7a7a\u5b57\u7b26\u4e32&#8221;\uff0c\u867d\u7136\u63d0\u793awarning\uff0c\u4f46\u5e76\u4e0d\u5f71\u54cd\u81ea\u589e\u4e3b\u952e\u7684\u751f\u6210\u3002<\/p>\n<p>\u4f46\u5728MySQL 5.7\u4e2d\uff0c\u5176\u5c31\u8c03\u6574\u4e3a\u4e86\u4e25\u683c\u6a21\u5f0f\uff0c\u5bf9\u4e8e\u4e0a\u9762\u8fd9\u4e2a\uff0c\u5176\u4e0d\u4f1a\u63d0\u793awarning\uff0c\u800c\u662f\u76f4\u63a5\u62a5\u9519\u3002<\/p>\n<p>\u5206\u7ec4\u6c42\u6700\u503c<\/p>\n<p>\u5206\u7ec4\u6c42\u6700\u503c\u7684\u67d0\u4e9b\u5199\u6cd5\u5728MySQL5.7\u4e2d\u5f97\u4e0d\u5230\u9884\u671f\u7ed3\u679c\uff0c\u8fd9\u70b9\uff0c\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u9690\u853d\u3002<\/p>\n<p>\u5176\u4e2d\uff0c\u7b2c\u4e00\u70b9\u662f\u53ef\u63a7\u7684\uff0c\u6bd5\u7adf\u53ef\u4ee5\u8c03\u6574\u53c2\u6570\u3002\u800c\u7b2c\u4e8c\u70b9\uff0c\u5374\u662f\u4e0d\u53ef\u63a7\u7684\uff0c\u6ca1\u6709\u53c2\u6570\u4e0e\u4e4b\u76f8\u5173\uff0c\u9700\u8981\u5f00\u53d1Review\u4ee3\u7801\u3002<\/p>\n<p>\u4e0b\u9762\u5177\u4f53\u6765\u770b\u770b<\/p>\n<p>\u6d4b\u8bd5\u6570\u636e<\/p>\n<p>mysql&gt; select * from emp;<br \/>\n+&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+<br \/>\n| empno | ename\u00a0 \u00a0 | sal\u00a0 \u00a0 | deptno |<br \/>\n+&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+<br \/>\n|\u00a0 1001 | emp_1001 | 100.00 |\u00a0 \u00a0 10 |<br \/>\n|\u00a0 1002 | emp_1002 | 200.00 |\u00a0 \u00a0 10 |<br \/>\n|\u00a0 1003 | emp_1003 | 300.00 |\u00a0 \u00a0 20 |<br \/>\n|\u00a0 1004 | emp_1004 | 400.00 |\u00a0 \u00a0 20 |<br \/>\n|\u00a0 1005 | emp_1005 | 500.00 |\u00a0 \u00a0 30 |<br \/>\n|\u00a0 1006 | emp_1006 | 600.00 |\u00a0 \u00a0 30 |<br \/>\n+&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+<br \/>\n6 rows in set (0.00 sec)<\/p>\n<p>\u5176\u4e2d\uff0cempno\u662f\u5458\u5de5\u7f16\u53f7\uff0cename\u662f\u5458\u5de5\u59d3\u540d\uff0csal\u662f\u5de5\u8d44\uff0cdeptno\u662f\u5458\u5de5\u6240\u5728\u90e8\u95e8\u53f7\u3002<\/p>\n<p>\u4e1a\u52a1\u7684\u9700\u6c42\u662f\uff0c\u6c42\u51fa\u6bcf\u4e2a\u90e8\u95e8\u4e2d\u5de5\u8d44\u6700\u9ad8\u7684\u5458\u5de5\u7684\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<p>\u5728MySQL5.6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u8fd9\u4e2aSQL\u6765\u5b9e\u73b0\uff0c<\/p>\n<p>SELECT<br \/>\ndeptno,ename,sal<br \/>\nFROM<br \/>\n( SELECT * FROM emp ORDER BY sal DESC ) t<br \/>\nGROUP BY<br \/>\ndeptno;<\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u5176\u786e\u5b9e\u5b9e\u73b0\u4e86\u9884\u671f\u6548\u679c\u3002<\/p>\n<p>+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+<br \/>\n| deptno | ename\u00a0 \u00a0 | sal\u00a0 \u00a0 |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+<br \/>\n|\u00a0 \u00a0 10 | emp_1002 | 200.00 |<br \/>\n|\u00a0 \u00a0 20 | emp_1004 | 400.00 |<br \/>\n|\u00a0 \u00a0 30 | emp_1006 | 600.00 |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+<\/p>\n<p>\u518d\u6765\u770b\u770bMySQL5.7\u7684\u7ed3\u679c\uff0c\u7adf\u7136\u4e0d\u4e00\u6837\u3002<\/p>\n<p>+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+<br \/>\n| deptno | ename\u00a0 \u00a0 | sal\u00a0 \u00a0 |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+<br \/>\n|\u00a0 \u00a0 10 | emp_1001 | 100.00 |<br \/>\n|\u00a0 \u00a0 20 | emp_1003 | 300.00 |<br \/>\n|\u00a0 \u00a0 30 | emp_1005 | 500.00 |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u5728MySQL5.7\u4e2d\uff0c\u5bf9\u8be5SQL\u8fdb\u884c\u4e86\u6539\u5199\uff0c\u6539\u5199\u540e\u7684SQL\u53ef\u901a\u8fc7explain(extended) + show warnings\u67e5\u770b\u3002<\/p>\n<p>mysql&gt; explain select deptno,ename,sal from (select * from emp order by sal desc) t group by deptno;<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n| id | select_type | table | partitions | type | possible_keys | key\u00a0 | key_len | ref\u00a0 | rows | filtered | Extra\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n|\u00a0 1 | SIMPLE\u00a0 \u00a0 \u00a0 | emp\u00a0 | NULL\u00a0 \u00a0 \u00a0 | ALL\u00a0 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | NULL | NULL\u00a0 \u00a0 | NULL |\u00a0 \u00a0 6 |\u00a0 100.00 | Using temporary |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n1 row in set, 1 warning (0.00 sec)<\/p>\n<p>mysql&gt; show warnings\\G<br \/>\n*************************** 1. row ***************************<br \/>\nLevel: Note<br \/>\nCode: 1003<br \/>\nMessage: \/* select#1 *\/ select `slowtech`.`emp`.`deptno` AS `deptno`,`slowtech`.`emp`.`ename` AS `ename`,`slowtech`.`emp`.`sal` AS `sal` from `slowtech`.`emp` group by `slowtech`.`emp`.`deptno`<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>\u4ece\u6539\u5199\u540e\u7684SQL\u6765\u770b\uff0c\u5176\u6d88\u9664\u4e86\u5b50\u67e5\u8be2\uff0c\u5bfc\u81f4\u7ed3\u679c\u672a\u80fd\u5b9e\u73b0\u9884\u671f\u6548\u679c\uff0c\u5b98\u65b9\u4e5f\u8bc1\u5b9e\u4e86\u8fd9\u4e00\u70b9\uff0chttps:\/\/bugs.mysql.com\/bug.php?id=80131<\/p>\n<p>\u5f88\u591a\u4eba\u53ef\u80fd\u4e0d\u4ee5\u4e3a\u7136\uff0c\u8ba4\u4e3a\u6ca1\u4eba\u4f1a\u8fd9\u6837\u5199\uff0c\u4f46\u5728\u5927\u540d\u9f0e\u9f0e\u7684stackoverflow\u4e2d\uff0c\u8be5\u5b9e\u73b0\u7684\u70b9\u8d5e\u6570\u5c31\u6709116\u4e2a-\u7531\u6b64\u53ef\u89c1\u5176\u53d7\u4f17\u4e4b\u5e7f\uff0c\u4ec5\u6b21\u4e8e\u540e\u9762\u63d0\u5230\u7684\u201c\u65b9\u6cd5\u4e8c\u201d\uff08\u70b9\u8d5e\u6570206\u4e2a\uff09\u3002<br \/>\nhttps:\/\/stackoverflow.com\/questions\/12102200\/get-records-with-max-value-for-each-group-of-grouped-sql-results<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8be5SQL\u57285.7\u4e2d\u662f\u4e0d\u80fd\u76f4\u63a5\u8fd0\u884c\u7684\uff0c\u5176\u4f1a\u63d0\u793a\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<p>ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column &#8216;t.ename&#8217; which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by<\/p>\n<p>\u8fd9\u4e2a\u4e0esql_mode\u6709\u5173\uff0c\u5728MySQL 5.7\u4e2d\uff0csql_mode\u8c03\u6574\u4e3a\u4e86<\/p>\n<p>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<\/p>\n<p>\u5176\u4e2d\uff0cONLY_FULL_GROUP_BY\u4e0egroup by\u8bed\u53e5\u6709\u5173\uff0c\u5176\u8981\u6c42select\u5217\u8868\u91cc\u53ea\u80fd\u51fa\u73b0\u5206\u7ec4\u5217\uff08\u5373group by\u540e\u9762\u7684\u5217\uff09\u548c\u805a\u5408\u51fd\u6570\uff08sum\uff0cavg\uff0cmax\u7b49\uff09\uff0c\u8fd9\u4e5f\u662fSQL92\u7684\u6807\u51c6\u3002<\/p>\n<p>\u4f46\u5728\u5de5\u4f5c\u4e2d\uff0c\u5374\u7ecf\u5e38\u770b\u5230\u5f00\u53d1\u5199\u51fa\u4e0b\u9762\u8fd9\u79cdSQL\u3002<\/p>\n<p>mysql&gt; select deptno,ename,max(sal) from emp group by deptno;<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n| deptno | ename\u00a0 \u00a0 | max(sal) |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n|\u00a0 \u00a0 10 | emp_1001 |\u00a0 200.00 |<br \/>\n|\u00a0 \u00a0 20 | emp_1003 |\u00a0 400.00 |<br \/>\n|\u00a0 \u00a0 30 | emp_1005 |\u00a0 600.00 |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+<br \/>\n3 rows in set (0.01 sec)<\/p>\n<p>\u5b9e\u5728\u4e0d\u660e\u767d\uff0c\u8fd9\u91cc\u7684ename\u5728\u4e1a\u52a1\u5c42\u6709\u4f55\u610f\u4e49\uff0c\u6bd5\u7adf\uff0c\u4ed6\u5e76\u4e0d\u662f\u5de5\u8d44\u6700\u9ad8\u7684\u90a3\u4f4d\u5458\u5de5\u3002<\/p>\n<p>\u5206\u7ec4\u6c42\u6700\u503c\uff0cMySQL\u7684\u5b9e\u73b0\u65b9\u5f0f<\/p>\n<p>\u5176\u5b9e\u5206\u7ec4\u6c42\u6700\u503c\u662f\u4e00\u4e2a\u5f88\u666e\u904d\u7684\u9700\u6c42\u3002\u5728\u5de5\u4f5c\u4e2d\uff0c\u4e5f\u7ecf\u5e38\u88ab\u5f00\u53d1\u540c\u4e8b\u95ee\u5230\u3002 \u4e0b\u9762\u5177\u4f53\u6765\u770b\u770b\uff0cMySQL\u4e2d\u6709\u54ea\u4e9b\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u65b9\u6cd51<\/p>\n<p>SELECT<br \/>\ne.deptno,<br \/>\nename,<br \/>\nsal<br \/>\nFROM<br \/>\nemp e,<br \/>\n( SELECT deptno, max( sal ) maxsal FROM emp GROUP BY deptno ) t<br \/>\nWHERE<br \/>\ne.deptno = t.deptno<br \/>\nAND e.sal = t.maxsal;<\/p>\n<p>\u65b9\u6cd52<\/p>\n<p>SELECT<br \/>\na.deptno,<br \/>\na.ename,<br \/>\na.sal<br \/>\nFROM<br \/>\nemp a<br \/>\nLEFT JOIN emp b ON a.deptno = b.deptno<br \/>\nAND a.sal &lt; b.sal<br \/>\nWHERE<br \/>\nb.sal IS NULL;<\/p>\n<p>\u8fd9\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5176\u5b9e\u662f\u901a\u7528\u7684\uff0c\u4e0d\u4ec5\u9002\u7528\u4e8eMySQL\uff0c\u4e5f\u9002\u7528\u4e8e\u5176\u5b83\u4e3b\u6d41\u5173\u7cfb\u578b\u6570\u636e\u5e93\u3002<\/p>\n<p>\u65b9\u6cd53<br \/>\nMySQL 8.0\u63a8\u51fa\u4e86\u5206\u6790\u51fd\u6570\uff0c\u5176\u4e5f\u53ef\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\u3002<\/p>\n<p>SELECT<br \/>\ndeptno,<br \/>\nename,<br \/>\nsal<br \/>\nFROM<br \/>\n(<br \/>\nSELECT<br \/>\ndeptno,<br \/>\nename,<br \/>\nsal,<br \/>\nLAST_VALUE ( sal ) OVER ( PARTITION BY deptno ORDER BY sal ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) maxsal<br \/>\nFROM<br \/>\nemp<br \/>\n) a<br \/>\nWHERE<br \/>\nsal = maxsal;<\/p>\n<p>\u4e09\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u6027\u80fd\u5bf9\u6bd4<\/p>\n<p>\u56e0\u4e0a\u9762\u6d4b\u8bd5\u6848\u4f8b\u7684\u6570\u636e\u91cf\u592a\u5c0f\uff0c\u4e09\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u7ed3\u679c\u90fd\u662f\u79d2\u51fa\uff0c\u4ec5\u51ed\u6267\u884c\u8ba1\u5212\u5f88\u96be\u76f4\u89c2\u5730\u770b\u51fa\u5b9e\u73b0\u65b9\u5f0f\u7684\u4f18\u52a3\u3002<\/p>\n<p>\u4e0b\u9762\u6362\u4e0a\u6570\u636e\u91cf\u66f4\u5927\u7684\u6d4b\u8bd5\u6570\u636e\uff0c\u5b98\u65b9\u793a\u4f8b\u6570\u636e\u5e93employees\u4e2d\u7684dept_emp\u8868\uff0chttps:\/\/github.com\/datacharmer\/test_db<\/p>\n<p>\u8868\u7684\u76f8\u5173\u4fe1\u606f\u5982\u4e0b\uff0c\u5176\u4e2demp_no\u662f\u5458\u5de5\u7f16\u53f7\uff0cdept_no\u662f\u90e8\u95e8\u7f16\u53f7\uff0cfrom_date\u662f\u5165\u804c\u65e5\u671f\u3002<\/p>\n<p>mysql&gt; show create table dept_emp\\G<br \/>\n*************************** 1. row ***************************<br \/>\nTable: dept_emp<br \/>\nCreate Table: CREATE TABLE `dept_emp` (<br \/>\n`emp_no` int(11) NOT NULL,<br \/>\n`dept_no` char(4) NOT NULL,<br \/>\n`from_date` date NOT NULL,<br \/>\n`to_date` date NOT NULL,<br \/>\nKEY `dept_no` (`dept_no`,`from_date`)<br \/>\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>mysql&gt; select count(*) from dept_emp;<br \/>\n+&#8212;&#8212;&#8212;-+<br \/>\n| count(*) |<br \/>\n+&#8212;&#8212;&#8212;-+<br \/>\n|\u00a0 331603 |<br \/>\n+&#8212;&#8212;&#8212;-+<br \/>\n1 row in set (0.09 sec)<\/p>\n<p>mysql&gt; select * from dept_emp limit 1;<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<br \/>\n| emp_no | dept_no | from_date\u00a0 | to_date\u00a0 \u00a0 |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<br \/>\n|\u00a0 10001 | d005\u00a0 \u00a0 | 1986-06-26 | 9999-01-01 |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>\u65b9\u6cd51<\/p>\n<p>mysql&gt; select d.dept_no,d.emp_no,d.from_date from dept_emp d, (select dept_no,max(from_date) max_hiredate from dept_emp group by dept_no) t where d.dept_no=t.dept_no and d.from_date=t.max_hiredate;<br \/>\n\u2026<br \/>\n12 rows in set (0.00 sec)<\/p>\n<p>mysql&gt; explain select d.dept_no,d.emp_no,d.from_date from dept_emp d, (select dept_no,max(from_date) max_hiredate from dept_emp group by dept_no) t where d.dept_no=t.dept_no and d.from_date=t.max_hiredate;<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n| id | select_type | table\u00a0 \u00a0 \u00a0 | partitions | type\u00a0 | possible_keys | key\u00a0 \u00a0 | key_len | ref\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | rows | filtered | Extra<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n|\u00a0 1 | PRIMARY\u00a0 \u00a0 | &lt;derived2&gt; | NULL\u00a0 \u00a0 \u00a0 | ALL\u00a0 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | NULL\u00a0 \u00a0 | NULL\u00a0 \u00a0 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |\u00a0 \u00a0 9 |\u00a0 100.00 | Using where<br \/>\n|\u00a0 1 | PRIMARY\u00a0 \u00a0 | d\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | NULL\u00a0 \u00a0 \u00a0 | ref\u00a0 | dept_no\u00a0 \u00a0 \u00a0 | dept_no | 19\u00a0 \u00a0 \u00a0 | t.dept_no,t.max_hiredate |\u00a0 \u00a0 5 |\u00a0 100.00 | NULL<br \/>\n|\u00a0 2 | DERIVED\u00a0 \u00a0 | dept_emp\u00a0 | NULL\u00a0 \u00a0 \u00a0 | range | dept_no\u00a0 \u00a0 \u00a0 | dept_no | 16\u00a0 \u00a0 \u00a0 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |\u00a0 \u00a0 9 |\u00a0 100.00 | Using index for group-by<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>\u65b9\u6cd52<\/p>\n<p>mysql&gt; explain select a.dept_no,a.emp_no,a.from_date from dept_emp a left join dept_emp b on a.dept_no=b.dept_no and a.from_date &lt; b.from_date where b.from_date is null;<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n| id | select_type | table | partitions | type | possible_keys | key\u00a0 \u00a0 | key_len | ref\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | rows\u00a0 | filtered | Extra\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n|\u00a0 1 | SIMPLE\u00a0 \u00a0 \u00a0 | a\u00a0 \u00a0 | NULL\u00a0 \u00a0 \u00a0 | ALL\u00a0 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | NULL\u00a0 \u00a0 | NULL\u00a0 \u00a0 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | 331008 |\u00a0 100.00 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |<br \/>\n|\u00a0 1 | SIMPLE\u00a0 \u00a0 \u00a0 | b\u00a0 \u00a0 | NULL\u00a0 \u00a0 \u00a0 | ref\u00a0 | dept_no\u00a0 \u00a0 \u00a0 | dept_no | 16\u00a0 \u00a0 \u00a0 | slowtech.a.dept_no |\u00a0 41376 |\u00a0 \u00a0 19.00 | Using where; Using index |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n2 rows in set, 1 warning (0.00 sec)<\/p>\n<p>\u65b9\u6cd53<\/p>\n<p>mysql&gt; select dept_no,emp_no,from_date from ( select dept_no,emp_no,from_date,last_value(from_date) over(partition by dept_no order by from_date rows between unbounded preceding and unbounded following) max_hiredate from dept_emp) a where from_date=max_hiredate;<br \/>\n\u2026<br \/>\n12 rows in set (1.57 sec)<\/p>\n<p>mysql&gt; desc select dept_no,emp_no,from_date from ( select dept_no,emp_no,from_date,last_value(from_date) over(partition by dept_no order by from_date rows between unbounded preceding and unbounded following) max_hiredate from dept_emp) a where from_date=max_hiredate;<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| id | select_type | table\u00a0 \u00a0 \u00a0 | partitions | type | possible_keys | key\u00a0 | key_len | ref\u00a0 | rows\u00a0 | filtered | Extra\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n|\u00a0 1 | PRIMARY\u00a0 \u00a0 | &lt;derived2&gt; | NULL\u00a0 \u00a0 \u00a0 | ALL\u00a0 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | NULL | NULL\u00a0 \u00a0 | NULL | 331008 |\u00a0 100.00 | Using where\u00a0 \u00a0 |<br \/>\n|\u00a0 2 | DERIVED\u00a0 \u00a0 | dept_emp\u00a0 | NULL\u00a0 \u00a0 \u00a0 | ALL\u00a0 | NULL\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | NULL | NULL\u00a0 \u00a0 | NULL | 331008 |\u00a0 100.00 | Using filesort |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n2 rows in set, 2 warnings (0.00 sec)<\/p>\n<p>\u4ece\u6267\u884c\u65f6\u95f4\u4e0a\u770b\uff0c<\/p>\n<p>\u65b9\u6cd51\u7684\u65f6\u95f4\u6700\u77ed\uff0c\u5728\u6709\u590d\u5408\u7d22\u5f15(deptno, fromdate)\u7684\u60c5\u51b5\u4e0b\uff0c\u7ed3\u679c\u77ac\u95f4\u5c31\u51fa\u6765\u4e86\uff0c\u5373\u4f7f\u5728\u6ca1\u6709\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\uff0c\u4e5f\u53ea\u6d88\u8017\u4e860.75s\u3002<\/p>\n<p>\u65b9\u6cd52\u7684\u65f6\u95f4\u6700\u957f\uff0c3\u4e2a\u5c0f\u65f6\u8fd8\u662f\u6ca1\u51fa\u7ed3\u679c\u3002\u540c\u6837\u7684\u6570\u636e\uff0c\u540c\u6837\u7684SQL\uff0c\u653e\u5230Oracle\u67e5\uff0c\u4e5f\u6d88\u8017\u4e8687\u520649\u79d2\u3002<\/p>\n<p>\u65b9\u6cd53\u7684\u65f6\u95f4\u6bd4\u8f83\u56fa\u5b9a\uff0c\u65e0\u8bba\u662f\u5426\u5b58\u5728\u7d22\u5f15\uff0c\u90fd\u7ef4\u6301\u57281.5s\u5de6\u53f3\uff0c\u6bd4\u65b9\u6cd51\u7684\u8017\u65f6\u8981\u4e45\u3002<\/p>\n<p>\u8fd9\u91cc\uff0c\u5bf9\u4e4b\u524d\u63d0\u5230\u7684\uff0cMySQL 5.7\u4e2d\u4e0d\u518d\u517c\u5bb9\u7684\u5b9e\u73b0\u65b9\u5f0f\u4e5f\u505a\u4e86\u4e2a\u6d4b\u8bd5\uff0c\u5728\u6ca1\u6709\u4efb\u4f55\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\uff0c\u5176\u7a33\u5b9a\u57280.7s\uff08\u6027\u80fd\u5e76\u4e0d\u5f31\uff0c\u602a\u4e0d\u5f97\u6709\u4eba\u4f7f\u7528\uff09\uff0c\u800c\u540c\u7b49\u60c5\u51b5\u4e0b\uff0c\u65b9\u6cd51\u7a33\u5b9a\u57280.5s\uff08\u54c8\uff0cMySQL 5.6\u7adf\u7136\u6bd48.0\u8fd8\u5feb\uff09\u3002\u4f46\u4e0e\u65b9\u6cd51\u4e0d\u540c\u7684\u662f\uff0c\u5176\u65e0\u6cd5\u901a\u8fc7\u7d22\u5f15\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<p>\u4ece\u6267\u884c\u8ba1\u5212\u4e0a\u770b\uff0c<\/p>\n<p>\u65b9\u6cd51\uff0c \u5148\u5c06group by\u7684\u7ed3\u679c\u653e\u5230\u4e34\u65f6\u8868\u4e2d\uff0c\u7136\u540e\u518d\u5c06\u8be5\u4e34\u65f6\u8868\u4f5c\u4e3a\u9a71\u52a8\u8868\uff0c\u6765\u548cdept_emp\u8868\u8fdb\u884c\u5173\u8054\u67e5\u8be2\u3002\u9a71\u52a8\u8868\u5c0f\uff08\u53ea\u67099\u6761\u8bb0\u5f55\uff09\uff0c\u5173\u8054\u5217\u53c8\u6709\u7d22\u5f15\uff0c\u65e0\u602a\u4e4e\uff0c\u7ed3\u679c\u80fd\u79d2\u51fa\u3002<\/p>\n<p>\u65b9\u6cd52\uff0c \u4e24\u8868\u5173\u8054\u3002\u5176\u72af\u4e86SQL\u4f18\u5316\u4e2d\u7684\u4e24\u4e2a\u5927\u5fcc\u3002<\/p>\n<p>1. \u9a71\u52a8\u8868\u592a\u5927\uff0c\u5176\u6709331603\u6761\u8bb0\u5f55\u3002<\/p>\n<p>2. \u88ab\u9a71\u52a8\u8868\u867d\u7136\u4e5f\u6709\u7d22\u5f15\uff0c\u4f46\u4ece\u6267\u884c\u8ba1\u5212\u4e0a\u770b\uff0c\u5176\u53ea\u4f7f\u7528\u4e86\u590d\u5408\u7d22\u5f15\u00a0 (dept_no, from_date)\u4e2d\u7684dept_no\uff0c\u800cdept_no\u7684\u9009\u62e9\u7387\u53c8\u592a\u4f4e\uff0c\u6bd5\u7adf\u53ea\u67099\u4e2a\u90e8\u95e8\u3002<\/p>\n<p>\u65b9\u6cd53\uff0c \u5148\u628a\u5206\u6790\u7684\u7ed3\u679c\u653e\u5230\u4e00\u4e2a\u4e34\u65f6\u8868\u4e2d\uff0c\u7136\u540e\u518d\u5bf9\u8be5\u4e34\u65f6\u8868\u8fdb\u884c\u5904\u7406\u3002\u5176\u8fdb\u884c\u4e86\u4e24\u6b21\u5168\u8868\u626b\u63cf\uff0c\u4e00\u6b21\u662f\u9488\u5bf9dept_emp\u8868\uff0c\u4e00\u6b21\u662f\u9488\u5bf9\u4e34\u65f6\u8868\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5bf9\u4e8e\u5206\u7ec4\u6c42\u6700\u503c\u7684\u9700\u6c42\uff0c\u5efa\u8bae\u4f7f\u7528\u65b9\u6cd51\uff0c\u5176\u4e0d\u4ec5\u7b26\u5408SQL\u89c4\u8303\uff0c\u67e5\u8be2\u6027\u80fd\u4e0a\u4e5f\u662f\u6700\u597d\u7684\uff0c\u5c24\u5176\u662f\u5728\u8054\u5408\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p>PS\uff1a<\/p>\n<p>\u7ecf\u5927\u795e\u6307\u70b9\uff0c\u5bf9\u4e4b\u524d\u63d0\u5230\u7684\uff0cMySQL 5.7\u4e2d\u4e0d\u518d\u517c\u5bb9\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5b9e\u9645\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574optimizer_switch\u6765\u52a0\u4ee5\u89c4\u907f<\/p>\n<p>set optimizer_switch=&#8217;derived_merge=off&#8217;;<\/p>\n<p>derived_merge\u662fMySQL 5.7\u5f15\u5165\u7684\uff0c\u5176\u4f1a\u8bd5\u56fe\u5c06Derived Table\uff08\u6d3e\u751f\u8868\uff0cfrom\u540e\u9762\u7684\u5b50\u67e5\u8be2\uff09\uff0c\u89c6\u56fe\u5f15\u7528\uff0c\u516c\u7528\u8868\u8868\u8fbe\u5f0f\uff08Common table expressions\uff09\u4e0e\u5916\u5c42\u67e5\u8be2\u8fdb\u884c\u5408\u5e76\u3002\u5982\uff0c<\/p>\n<p>SELECT *<br \/>\nFROM t1 JOIN (SELECT t2.f1 FROM t2) AS derived_t2<br \/>\nON t1.f2=derived_t2.f1<br \/>\nWHERE t1.f1 &gt; 0;<\/p>\n<p>\u6539\u5199\u4e3a<\/p>\n<p>SELECT *<br \/>\nFROM t1 JOIN (SELECT DISTINCT f1 FROM t2) AS derived_t2<br \/>\nON t1.f1=derived_t2.f1;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\uff0c\u5c06\u7ebf\u4e0aMySQL\u6570\u636e\u5e93\u5347\u7ea7\u5230\u4e865.7\u3002\u8003\u8651\u5230\u53ef\u80fd\u4ea7\u751f\u7684\u4e0d\u517c\u5bb9\u6027\uff0c\u5728\u5347\u7ea7\u4e4b\u524d\uff0c\u786e\u5b9e\u4e5f\u662f\u6218\u6218\u5162\u5162\uff0c\u867d\u7136\u6d4b &#8230; <a title=\"\u5347\u7ea7MySQL5.7\uff0c\u5f00\u53d1\u4e0d\u5f97\u4e0d\u6ce8\u610f\u7684\u5751\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/jishu\/shujuku\/%e5%8d%87%e7%ba%a7mysql5-7%ef%bc%8c%e5%bc%80%e5%8f%91%e4%b8%8d%e5%be%97%e4%b8%8d%e6%b3%a8%e6%84%8f%e7%9a%84%e5%9d%91\/\" aria-label=\"\u9605\u8bfb \u5347\u7ea7MySQL5.7\uff0c\u5f00\u53d1\u4e0d\u5f97\u4e0d\u6ce8\u610f\u7684\u5751\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-3551","post","type-post","status-publish","format-standard","hentry","category-shujuku"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=3551"}],"version-history":[{"count":2,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3551\/revisions"}],"predecessor-version":[{"id":3598,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3551\/revisions\/3598"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=3551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=3551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=3551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}