{"id":132,"date":"2024-12-08T23:54:43","date_gmt":"2024-12-08T15:54:43","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=132"},"modified":"2024-12-08T23:54:43","modified_gmt":"2024-12-08T15:54:43","slug":"sql%e5%ba%94%e7%9f%a5%e5%ba%94%e4%bc%9a","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/08\/sql%e5%ba%94%e7%9f%a5%e5%ba%94%e4%bc%9a\/","title":{"rendered":"SQL\u5e94\u77e5\u5e94\u4f1a"},"content":{"rendered":"\n<p>\u4f5c\u4e3a\u6570\u636e\u5206\u6790\u4eba\u5458\uff0cSQL \u662f\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u638c\u63e1SQL\u7684\u57fa\u672c\u548c\u8fdb\u9636\u6280\u80fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u4e3a\u4e86\u66f4\u6e05\u695a\u5730\u4e86\u89e3\u201cSQL\u5e94\u77e5\u5e94\u4f1a\u201d\u7684\u6838\u5fc3\u6280\u80fd\uff0c\u4e0b\u9762\u8be6\u7ec6\u8bb2\u89e3\u4e86\u6bcf\u4e2a\u90e8\u5206\uff0c\u5e76\u7ed3\u5408\u793a\u4f8b\uff0c\u8ba9\u5185\u5bb9\u66f4\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u57fa\u672c\u67e5\u8be2\u6280\u80fd\uff08\u5fc5\u5907\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u5b66\u4f1a\u57fa\u672c\u7684\u67e5\u8be2\u64cd\u4f5c\uff0c\u80fd\u591f\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>SELECT<\/code><\/strong>\uff1a\u67e5\u8be2\u6570\u636e\u7684\u6838\u5fc3\u8bed\u53e5\uff0c\u5141\u8bb8\u4f60\u4ece\u6570\u636e\u5e93\u8868\u4e2d\u63d0\u53d6\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong><code>WHERE<\/code><\/strong>\uff1a\u7528\u4e8e\u8fc7\u6ee4\u6570\u636e\uff0c\u53ea\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><strong><code>ORDER BY<\/code><\/strong>\uff1a\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff0c\u9ed8\u8ba4\u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u4e5f\u53ef\u4ee5\u6307\u5b9a\u964d\u5e8f\u6392\u5e8f\u3002<\/li>\n\n\n\n<li><strong><code>LIMIT<\/code> \/ <code>TOP<\/code><\/strong>\uff1a\u9650\u5236\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\uff0c\u5e38\u7528\u4e8e\u5206\u9875\u67e5\u8be2\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u67e5\u8be2\u6240\u6709\u5458\u5de5\u7684\u59d3\u540d\u548c\u5e74\u9f84\uff0c\u6309\u5e74\u9f84\u964d\u5e8f\u6392\u5e8f\uff0c\u9650\u5236\u8fd4\u56de\u524d10\u6761\u8bb0\u5f55\n   SELECT name, age\n   FROM employees\n   WHERE age &gt; 30\n   ORDER BY age DESC\n   LIMIT 10;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u4e0a\u9762\u7684\u67e5\u8be2\u4f1a\u8fd4\u56de <code>employees<\/code> \u8868\u4e2d\u5e74\u9f84\u5927\u4e8e30\u5c81\u7684\u5458\u5de5\u59d3\u540d\u548c\u5e74\u9f84\uff0c\u7ed3\u679c\u6309\u5e74\u9f84\u964d\u5e8f\u6392\u5217\uff0c\u5e76\u4e14\u6700\u591a\u8fd4\u56de10\u6761\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u6570\u636e\u805a\u5408\u4e0e\u5206\u7ec4\uff08\u5fc5\u5907\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u4f7f\u7528\u805a\u5408\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u6c47\u603b\uff0c\u5e2e\u52a9\u4f60\u5206\u6790\u6570\u636e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u805a\u5408\u51fd\u6570<\/strong>\uff1a\u5982 <code>COUNT()<\/code>, <code>SUM()<\/code>, <code>AVG()<\/code>, <code>MIN()<\/code>, <code>MAX()<\/code> \u7528\u4e8e\u8ba1\u7b97\u6570\u636e\u7684\u603b\u548c\u3001\u5e73\u5747\u503c\u3001\u6700\u5c0f\u503c\u3001\u6700\u5927\u503c\u548c\u884c\u6570\u3002<\/li>\n\n\n\n<li><strong><code>GROUP BY<\/code><\/strong>\uff1a\u6309\u67d0\u4e2a\u5b57\u6bb5\u5c06\u6570\u636e\u5206\u7ec4\uff0c\u901a\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong><code>HAVING<\/code><\/strong>\uff1a\u5bf9\u805a\u5408\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\uff08\u7c7b\u4f3c <code>WHERE<\/code>\uff0c\u4f46\u4f5c\u7528\u4e8e <code>GROUP BY<\/code> \u540e\u7684\u7ed3\u679c\uff09\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u67e5\u8be2\u6bcf\u4e2a\u90e8\u95e8\u7684\u5458\u5de5\u6570\u91cf\n   SELECT department, COUNT(*) AS num_employees\n   FROM employees\n   GROUP BY department;<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u67e5\u8be2\u6bcf\u4e2a\u90e8\u95e8\u4e2d\u5458\u5de5\u6570\u5927\u4e8e5\u7684\u90e8\u95e8\n   SELECT department, COUNT(*) AS num_employees\n   FROM employees\n   GROUP BY department\n   HAVING COUNT(*) &gt; 5;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u7b2c\u4e00\u4e2a\u67e5\u8be2\u6309\u90e8\u95e8\u5bf9\u5458\u5de5\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u90e8\u95e8\u7684\u5458\u5de5\u6570\u91cf\u3002\u7b2c\u4e8c\u4e2a\u67e5\u8be2\u5bf9\u805a\u5408\u7ed3\u679c\u8fdb\u884c\u7b5b\u9009\uff0c\u53ea\u663e\u793a\u5458\u5de5\u6570\u5927\u4e8e5\u7684\u90e8\u95e8\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u591a\u8868\u67e5\u8be2\u4e0e\u8fde\u63a5\uff08\u5fc5\u5907\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u4ece\u591a\u4e2a\u8868\u4e2d\u63d0\u53d6\u6570\u636e\uff0c\u901a\u8fc7\u8fde\u63a5\u67e5\u8be2\u4e0d\u540c\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>INNER JOIN<\/code><\/strong>\uff1a\u8fd4\u56de\u5728\u4e24\u4e2a\u8868\u4e2d\u90fd\u5b58\u5728\u7684\u5339\u914d\u6570\u636e\uff08\u6700\u5e38\u7528\u7684\u8fde\u63a5\u7c7b\u578b\uff09\u3002<\/li>\n\n\n\n<li><strong><code>LEFT JOIN<\/code> \/ <code>RIGHT JOIN<\/code><\/strong>\uff1a\u8fd4\u56de\u5de6\u8868\uff08\u6216\u53f3\u8868\uff09\u7684\u6240\u6709\u6570\u636e\uff0c\u5373\u4f7f\u53f3\u8868\uff08\u6216\u5de6\u8868\uff09\u6ca1\u6709\u5339\u914d\u3002<\/li>\n\n\n\n<li><strong><code>FULL OUTER JOIN<\/code><\/strong>\uff1a\u8fd4\u56de\u5de6\u8868\u548c\u53f3\u8868\u4e2d\u6240\u6709\u7684\u6570\u636e\uff0c\u4e0d\u8bba\u662f\u5426\u5339\u914d\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u67e5\u8be2\u5458\u5de5\u59d3\u540d\u53ca\u5176\u6240\u5728\u90e8\u95e8\u7684\u540d\u79f0\n   SELECT employees.name, departments.department_name\n   FROM employees\n   INNER JOIN departments\n   ON employees.department_id = departments.id;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u6b64\u67e5\u8be2\u901a\u8fc7 <code>INNER JOIN<\/code> \u5c06 <code>employees<\/code> \u8868\u548c <code>departments<\/code> \u8868\u8fde\u63a5\u8d77\u6765\uff0c\u8fd4\u56de\u6bcf\u4e2a\u5458\u5de5\u7684\u59d3\u540d\u53ca\u5176\u6240\u5728\u90e8\u95e8\u7684\u540d\u79f0\u3002\u8fde\u63a5\u6761\u4ef6\u662f\u901a\u8fc7 <code>department_id<\/code> \u548c <code>id<\/code> \u5b57\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5b50\u67e5\u8be2\uff08\u5fc5\u5907\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u5b66\u4f1a\u5d4c\u5957\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\uff09\uff0c\u901a\u8fc7\u5728\u67e5\u8be2\u4e2d\u5d4c\u5165\u5176\u4ed6\u67e5\u8be2\u6765\u63d0\u53d6\u590d\u6742\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5d4c\u5957\u67e5\u8be2<\/strong>\uff1a\u5c06\u4e00\u4e2a\u67e5\u8be2\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u67e5\u8be2\u7684\u6761\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u76f8\u5173\u5b50\u67e5\u8be2<\/strong>\uff1a\u5b50\u67e5\u8be2\u5f15\u7528\u5916\u90e8\u67e5\u8be2\u4e2d\u7684\u5b57\u6bb5\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u9010\u884c\u5bf9\u6bd4\u7684\u573a\u666f\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u67e5\u8be2\u6240\u6709\u5458\u5de5\u7684\u59d3\u540d\u548c\u4ed6\u4eec\u6240\u5728\u7684\u90e8\u95e8\u540d\u79f0\uff08\u901a\u8fc7\u5b50\u67e5\u8be2\u83b7\u5f97\u90e8\u95e8ID\uff09\n   SELECT name, \n          (SELECT department_name FROM departments WHERE id = employees.department_id) AS department_name\n   FROM employees;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u8fd9\u4e2a\u67e5\u8be2\u4e2d\uff0c<code>SELECT<\/code> \u5b50\u53e5\u901a\u8fc7\u4e00\u4e2a\u5b50\u67e5\u8be2\u6765\u67e5\u627e\u6bcf\u4e2a\u5458\u5de5\u7684\u90e8\u95e8\u540d\u79f0\u3002\u5916\u90e8\u67e5\u8be2\u68c0\u7d22\u5458\u5de5\u4fe1\u606f\uff0c\u5185\u90e8\u5b50\u67e5\u8be2\u6839\u636e <code>department_id<\/code> \u67e5\u627e\u90e8\u95e8\u540d\u79f0\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5b57\u7b26\u4e32\u548c\u65e5\u671f\u64cd\u4f5c\uff08\u5e38\u7528\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u80fd\u591f\u5904\u7406\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u3001\u65e5\u671f\u6570\u636e\uff0c\u8fdb\u884c\u5fc5\u8981\u7684\u6570\u636e\u8f6c\u6362\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b57\u7b26\u4e32\u51fd\u6570<\/strong>\uff1a\u5982 <code>CONCAT()<\/code> \u5408\u5e76\u5b57\u7b26\u4e32\uff0c<code>SUBSTRING()<\/code> \u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\uff0c<code>TRIM()<\/code> \u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3002<\/li>\n\n\n\n<li><strong>\u65e5\u671f\u51fd\u6570<\/strong>\uff1a\u5982 <code>NOW()<\/code> \u83b7\u53d6\u5f53\u524d\u65e5\u671f\uff0c<code>DATE_ADD()<\/code> \u548c <code>DATE_SUB()<\/code> \u8fdb\u884c\u65e5\u671f\u52a0\u51cf\uff0c<code>DATEDIFF()<\/code> \u8ba1\u7b97\u65e5\u671f\u5dee\u5f02\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u62fc\u63a5\u5458\u5de5\u7684\u540d\u5b57\u548c\u59d3\u6c0f\n   SELECT CONCAT(first_name, ' ', last_name) AS full_name\n   FROM employees;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u67e5\u8be2\u5165\u804c\u65f6\u95f4\u8ddd\u79bb\u4eca\u5929\u8d85\u8fc710\u5e74\u7684\u5458\u5de5\n   SELECT name, hire_date\n   FROM employees\n   WHERE DATEDIFF(CURDATE(), hire_date) &gt; 3650;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u7b2c\u4e00\u4e2a\u67e5\u8be2\u5c06\u5458\u5de5\u7684\u540d\u5b57\u548c\u59d3\u6c0f\u5408\u5e76\u4e3a\u5b8c\u6574\u59d3\u540d\uff1b\u7b2c\u4e8c\u4e2a\u67e5\u8be2\u8ba1\u7b97\u5165\u804c\u8d85\u8fc710\u5e74\u7684\u5458\u5de5\uff08\u5047\u8bbe\u4e00\u5e74365\u5929\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u66f4\u65b0\u4e0e\u5220\u9664\u6570\u636e\uff08\u5fc5\u5907\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u5728\u6570\u636e\u5e93\u4e2d\u4fee\u6539\u548c\u5220\u9664\u6570\u636e\uff0c\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>UPDATE<\/code><\/strong>\uff1a\u4fee\u6539\u8868\u4e2d\u5df2\u6709\u7684\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong><code>DELETE<\/code><\/strong>\uff1a\u5220\u9664\u8868\u4e2d\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><strong><code>INSERT INTO<\/code><\/strong>\uff1a\u5411\u8868\u4e2d\u63d2\u5165\u65b0\u6570\u636e\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u5c06\u90e8\u95e8\u4e3aHR\u7684\u6240\u6709\u5458\u5de5\u7684\u5de5\u8d44\u589e\u52a010%\n   UPDATE employees \n   SET salary = salary * 1.1\n   WHERE department = 'HR';<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u5220\u9664\u5165\u804c\u8d85\u8fc730\u5e74\u7684\u5458\u5de5\u8bb0\u5f55\n   DELETE FROM employees\n   WHERE DATEDIFF(CURDATE(), hire_date) &gt; 365 * 30;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u7b2c\u4e00\u4e2a\u67e5\u8be2\u4f1a\u5c06\u6240\u6709HR\u90e8\u95e8\u5458\u5de5\u7684\u5de5\u8d44\u589e\u52a010%\u3002\u7b2c\u4e8c\u4e2a\u67e5\u8be2\u4f1a\u5220\u9664\u5165\u804c\u8d85\u8fc730\u5e74\u7684\u5458\u5de5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u6027\u80fd\u4f18\u5316\uff08\u4e2d\u7ea7\u6280\u80fd\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u7406\u89e3\u5982\u4f55\u4f18\u5316SQL\u67e5\u8be2\uff0c\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u7d22\u5f15<\/strong>\uff1a\u7d22\u5f15\u80fd\u663e\u8457\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\u3002<\/li>\n\n\n\n<li><strong><code>EXPLAIN<\/code><\/strong>\uff1a\u67e5\u770bSQL\u67e5\u8be2\u6267\u884c\u8ba1\u5212\uff0c\u5e2e\u52a9\u4f60\u4f18\u5316\u67e5\u8be2\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   EXPLAIN SELECT * FROM employees WHERE department = 'Sales';<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a<code>EXPLAIN<\/code> \u53ef\u4ee5\u5e2e\u52a9\u4f60\u5206\u6790\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\u3002\u5b83\u4f1a\u663e\u793a\u67e5\u8be2\u662f\u5982\u4f55\u6267\u884c\u7684\uff0c\u5305\u62ec\u626b\u63cf\u7684\u8868\u3001\u7d22\u5f15\u7684\u4f7f\u7528\u60c5\u51b5\u7b49\uff0c\u5e2e\u52a9\u4f60\u8bc6\u522b\u74f6\u9888\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u7a97\u53e3\u51fd\u6570\uff08\u4e2d\u7ea7\u6280\u80fd\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u8fdb\u884c\u9ad8\u7ea7\u6570\u636e\u5206\u6790\uff0c\u5982\u6392\u540d\u3001\u6ed1\u52a8\u7a97\u53e3\u7b49\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7a97\u53e3\u51fd\u6570<\/strong>\uff1a\u5982 <code>ROW_NUMBER()<\/code>, <code>RANK()<\/code>, <code>LEAD()<\/code>, <code>LAG()<\/code> \u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u3001\u6392\u540d\u3001\u4ee5\u53ca\u5b9e\u73b0\u6ed1\u52a8\u7a97\u53e3\u5206\u6790\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   -- \u4e3a\u6bcf\u4e2a\u90e8\u95e8\u5185\u7684\u5458\u5de5\u6309\u5de5\u8d44\u6392\u5e8f\u5e76\u6392\u540d\n   SELECT name, department, salary, \n          ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank\n   FROM employees;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u6b64\u67e5\u8be2\u4e3a\u6bcf\u4e2a\u90e8\u95e8\u7684\u5458\u5de5\u6839\u636e\u5de5\u8d44\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5458\u5de5\u5206\u914d\u4e00\u4e2a\u6392\u540d\u3002<code>PARTITION BY<\/code> \u6309\u90e8\u95e8\u8fdb\u884c\u5206\u533a\uff0c<code>ORDER BY<\/code> \u6309\u5de5\u8d44\u964d\u5e8f\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u4e8b\u52a1\u7ba1\u7406\uff08\u4e2d\u9ad8\u7ea7\u6280\u80fd\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u5b66\u4f1a\u7ba1\u7406\u6570\u636e\u5e93\u4e8b\u52a1\uff0c\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u52a1<\/strong>\uff1a\u7406\u89e3\u4e8b\u52a1\u7684ACID\uff08\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u79bb\u6027\u3001\u6301\u4e45\u6027\uff09\u7279\u6027\u3002<\/li>\n\n\n\n<li><strong><code>BEGIN<\/code><\/strong>, <strong><code>COMMIT<\/code><\/strong>, <strong><code>ROLLBACK<\/code><\/strong>\uff1a\u4e8b\u52a1\u7684\u5f00\u59cb\u3001\u63d0\u4ea4\u548c\u56de\u6eda\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   BEGIN;\n   UPDATE employees SET salary = salary * 1.1 WHERE department = 'Finance';\n   COMMIT;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u8fd9\u4e2a\u4e8b\u52a1\u5c06Finance\u90e8\u95e8\u7684\u5458\u5de5\u5de5\u8d44\u589e\u52a010%\u3002\u5982\u679c\u4e8b\u52a1\u6267\u884c\u6210\u529f\uff0c\u4f7f\u7528 <code>COMMIT<\/code> \u63d0\u4ea4\u3002\u5982\u679c\u51fa\u73b0\u95ee\u9898\uff0c\u4f7f\u7528 <code>ROLLBACK<\/code> \u64a4\u9500\u6240\u6709\u66f4\u6539\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>\u6570\u636e\u5efa\u6a21\u4e0e\u6570\u636e\u5e93\u8bbe\u8ba1\uff08\u8fdb\u9636\u6280\u80fd\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong> \u80fd\u591f\u8bbe\u8ba1\u5408\u7406\u7684\u6570\u636e\u5e93\u7ed3\u6784\u548c\u6570\u636e\u6a21\u578b\uff0c\u4f18\u5316\u6570\u636e\u5b58\u50a8\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5e93\u89c4\u8303\u5316<\/strong>\uff1a\u901a\u8fc7\u89c4\u8303\u5316\u8bbe\u8ba1\u51cf\u5c11\u5197\u4f59\uff0c\u63d0\u9ad8\u6570\u636e\u4e00\u81f4\u6027\u3002<\/li>\n\n\n\n<li><strong>\u53cd\u89c4\u8303\u5316<\/strong>\uff1a\u4e3a\u6027\u80fd\u4f18\u5316\u505a\u51fa\u59a5\u534f\uff0c\u907f\u514d\u8fc7\u591a\u7684JOIN\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>**<\/li>\n<\/ul>\n\n\n\n<p>\u661f\u578b\u4e0e\u96ea\u82b1\u6a21\u578b**\uff1a\u5e38\u7528\u4e8e\u6570\u636e\u4ed3\u5e93\u8bbe\u8ba1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9bSQL\u6280\u80fd\uff0c\u4f60\u80fd\u591f\u6709\u6548\u5730\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u3001\u5206\u6790\u548c\u5904\u7406\u6570\u636e\u3002\u540c\u65f6\uff0c\u7406\u89e3\u6570\u636e\u5e93\u7684\u8bbe\u8ba1\u548c\u4f18\u5316\u6280\u5de7\u4e5f\u80fd\u63d0\u5347\u4f60\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u6548\u7387\u548c\u80fd\u529b\u3002\u5728\u4e0d\u540c\u7684\u5de5\u4f5c\u9636\u6bb5\uff0c\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u590d\u6742\u5ea6\uff0c\u4f60\u53ef\u80fd\u4f1a\u9010\u6b65\u6d89\u8db3\u66f4\u590d\u6742\u7684\u67e5\u8be2\u548c\u4f18\u5316\u6280\u672f\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u662fSQL\u5e94\u77e5\u5e94\u4f1a\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u66f4\u591a\u4fe1\u606f\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u4e3a\u6570\u636e\u5206\u6790\u4eba\u5458\uff0cSQL \u662f\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u638c\u63e1SQL\u7684\u57fa\u672c\u548c\u8fdb\u9636\u6280\u80fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u4e3a\u4e86\u66f4\u6e05\u695a\u5730\u4e86\u89e3\u201cSQL\u5e94 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[49,48,50],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-sql","tag-49","tag-48","tag-50"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/132\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}