{"id":2087,"date":"2025-11-20T13:03:42","date_gmt":"2025-11-20T05:03:42","guid":{"rendered":"https:\/\/www.tqazy.com\/?p=2087"},"modified":"2025-11-20T15:08:37","modified_gmt":"2025-11-20T07:08:37","slug":"%e9%9d%a2%e8%af%95%e7%ac%94%e8%ae%b0%ef%bc%9amysql-%e7%9b%b8%e5%85%b303-sql%e8%af%ad%e6%b3%95%e4%b8%8e%e6%9f%a5%e8%af%a2%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.tqazy.com\/?p=2087","title":{"rendered":"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316"},"content":{"rendered":"<h1>SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316<\/h1>\n<blockquote>\n<p>\u56de\u76ee\u5f55\uff1a <a href=\"https:\/\/www.tqazy.com\/?p=1972\" target=\"_blank\">\u300a\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u5173\u76ee\u5f55\u300b<\/a><br \/>\n\u4e0a\u4e00\u7bc7\uff1a <a href=\"https:\/\/www.tqazy.com\/?p=2082\" target=\"_blank\">\u300a\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517302 &#8211; \u7d22\u5f15\u300b<\/a><\/p>\n<\/blockquote>\n<hr\/>\n<h1>01. \u5b50\u67e5\u8be2\u4e0e\u8fde\u63a5(<code>join<\/code>)\u67e5\u8be2<\/h1>\n<p><strong>\u5b50\u67e5\u8be2<\/strong>\uff1a\u662f\u5d4c\u5957\u5728\u4e3b\u67e5\u8be2\u4e2d\u7684\u67e5\u8be2\uff0c\u6309\u201c\u8fd4\u56de\u7ed3\u679c\u7c7b\u578b\u201d\u548c\u201c\u662f\u5426\u4f9d\u8d56\u4e3b\u67e5\u8be2\u201d\u5206\u7c7b\uff0c\u6838\u5fc3\u662f \u201c\u5b50\u7ed3\u679c\u9a71\u52a8\u4e3b\u67e5\u8be2\u201d\uff1b<br \/>\n<strong>\u8fde\u63a5\u67e5\u8be2<\/strong>\uff1a\u662f\u591a\u8868\u5173\u8054\uff0c\u6838\u5fc3\u662f \u201c\u901a\u8fc7\u5173\u8054\u6761\u4ef6\u5408\u5e76\u8868\u6570\u636e\u201d\uff0c\u6309\u201c\u662f\u5426\u4fdd\u7559\u4e0d\u5339\u914d\u884c\u201d\u5206\u7c7b\uff0c\u5e95\u5c42\u4f9d\u8d56\u9a71\u52a8\u8868\u548c\u88ab\u9a71\u52a8\u8868\u7684\u5173\u8054\u903b\u8f91\u3002<\/p>\n<p>\u4ee5\u4e0b\u7ed9\u51fa\u4e864\u79cd\u6838\u5fc3\u5b50\u67e5\u8be2\u548c5\u79cd\u6838\u5fc3\u8fde\u63a5\u67e5\u8be2\uff0c\u4ee5\u53ca\u4e00\u79cd\u5185\u90e8\u4f18\u5316\u67e5\u8be2\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>01. \u6807\u91cf\u5b50\u67e5\u8be2<\/h2>\n<p>\u8fd4\u56de\u5355\u4e2a\u503c\u3002<\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u5b50\u67e5\u8be2\u8fd4\u56de 1 \u884c 1 \u5217\uff0c\u53ef\u4f5c\u4e3a\u4e3b\u67e5\u8be2\u7684\u5b57\u6bb5\u6216\u6761\u4ef6\u503c\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u8ba2\u5355\u6570\u6700\u591a\u7684\u7528\u6237\u540d\u79f0\uff08\u5b50\u67e5\u8be2\u8fd4\u56de\u6700\u5927\u8ba2\u5355\u6570\uff09\nSELECT username FROM user WHERE user_id = (SELECT MAX(user_id) FROM order WHERE status = 1);<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u6781\u4f4e\uff0c\u4ec5\u6267\u884c 1 \u6b21\u5b50\u67e5\u8be2\uff0c\u7ed3\u679c\u76f4\u63a5\u4ee3\u5165\u4e3b\u67e5\u8be2\uff1b<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u4e3b\u67e5\u8be2\u9700\u4f9d\u8d56\u5355\u4e2a\u805a\u5408\u503c\uff08\u5982\u6700\u5927\u503c\u3001\u5e73\u5747\u503c\uff09\u6216\u7279\u5b9a\u5355\u884c\u6570\u636e\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>02. \u5217\u5b50\u67e5\u8be2<\/h2>\n<p>\u8fd4\u56de\u5355\u5217\u591a\u884c\u3002<\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u5b50\u67e5\u8be2\u8fd4\u56de 1 \u5217 N \u884c\uff0c\u4e3b\u67e5\u8be2\u7528<code>IN<\/code>\/<code>NOT IN<\/code>\/<code>ANY<\/code>\/<code>ALL<\/code>\u5339\u914d\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u6709\u672a\u652f\u4ed8\u8ba2\u5355\u7684\u7528\u6237\uff08\u5b50\u67e5\u8be2\u8fd4\u56de\u672a\u652f\u4ed8\u8ba2\u5355\u7684\u7528\u6237ID\u5217\u8868\uff09\nSELECT username FROM user WHERE user_id IN (SELECT DISTINCT user_id FROM order WHERE status = 0);<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u4e2d\u4f4e\uff0cMySQL 5.7 + \u4f1a\u5c06\u5176\u4f18\u5316\u4e3a \u201c\u534a\u8fde\u63a5\u201d\uff08\u907f\u514d\u9010\u884c\u5339\u914d\uff09\uff0c\u5b50\u67e5\u8be2\u7ed3\u679c\u4f1a\u88ab\u7269\u5316\uff08\u5b58\u5165\u4e34\u65f6\u8868\uff09;<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u4e3b\u67e5\u8be2\u9700\u5339\u914d\u591a\u4e2a\u5019\u9009\u503c\uff08\u5982 \u201c\u5728\u67d0\u4e2a\u96c6\u5408\u4e2d\u201d\uff09\uff0c\u5b50\u67e5\u8be2\u7ed3\u679c\u96c6\u4e0d\u5b9c\u8fc7\u5927\uff08\u5efa\u8bae 1 \u4e07\u884c\u5185\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>03. \u884c\u5b50\u67e5\u8be2<\/h2>\n<p>\u8fd4\u56de\u591a\u884c\u591a\u5217\u3002<\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u5b50\u67e5\u8be2\u8fd4\u56de N \u884c N \u5217\uff08\u901a\u5e38 1-2 \u5217\uff09\uff0c\u4e3b\u67e5\u8be2\u7528IN\u5339\u914d\u884c\u6570\u636e\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u4e0e\u201c\u7528\u6237\u5f20\u4e09\u7684\u4e0a\u6d77\u5730\u5740\u201d\u5b8c\u5168\u5339\u914d\u7684\u7528\u6237\uff08\u8fd4\u56deid\u548ccity\u4e24\u5217\uff09\nSELECT username FROM user WHERE (user_id, city) IN (SELECT user_id, city FROM user_addr WHERE username = &#039;\u5f20\u4e09&#039; AND city = &#039;\u4e0a\u6d77&#039;);<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u4e2d\uff0c\u5b50\u67e5\u8be2\u7ed3\u679c\u96c6\u9700\u6309\u884c\u5339\u914d\uff0c\u5efa\u8bae\u7ed3\u679c\u96c6\u63a7\u5236\u5728 1 \u5343\u884c\u5185\uff1b<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u4e3b\u67e5\u8be2\u9700\u5339\u914d \u201c\u591a\u5b57\u6bb5\u7ec4\u5408\u6761\u4ef6\u201d\uff08\u5982\u8054\u5408\u4e3b\u952e\u3001\u591a\u7ef4\u5ea6\u7b5b\u9009\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>04. \u5173\u8054\u5b50\u67e5\u8be2<\/h2>\n<p>\u4f9d\u8d56\u4e3b\u67e5\u8be2\u5b57\u6bb5\u3002<\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u5b50\u67e5\u8be2\u7528\u4e3b\u67e5\u8be2\u7684\u5b57\u6bb5\u4f5c\u4e3a\u6761\u4ef6\uff08<code>EXISTS<\/code>\u662f\u5178\u578b\u7528\u6cd5\uff09\uff0c\u9700\u7528\u8868\u522b\u540d\u533a\u5206\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u6709\u6709\u6548\u8ba2\u5355\u7684\u7528\u6237\uff08\u5b50\u67e5\u8be2\u4f9d\u8d56\u4e3b\u67e5\u8be2\u7684user_id\uff0c\u627e\u5230\u5339\u914d\u5373\u505c\u6b62\uff09\nSELECT username FROM user u WHERE EXISTS (SELECT 1 FROM order o WHERE o.user_id = u.user_id AND o.status = 1);<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u4e2d\u9ad8\uff0c\u4e3b\u67e5\u8be2\u6bcf\u626b\u63cf 1 \u884c\uff0c\u5b50\u67e5\u8be2\u89e6\u53d1 1 \u6b21\uff08\u7c7b\u4f3c \u201c\u5d4c\u5957\u5faa\u73af\u201d\uff09\uff1b\u82e5\u5b50\u67e5\u8be2\u5173\u8054\u5b57\u6bb5\u65e0\u7d22\u5f15\uff0c\u4f1a\u5bfc\u81f4\u5168\u8868\u626b\u63cf\uff08\u5982 10 \u4e07\u884c\u4e3b\u8868\u89e6\u53d1 10 \u4e07\u6b21\u5b50\u67e5\u8be2\uff09\uff1b<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u4e3b\u67e5\u8be2\u9700 \u201c\u5b58\u5728\u6027\u6821\u9a8c\u201d\uff08\u5982 \u201c\u662f\u5426\u6709\u76f8\u5173\u6570\u636e\u201d\uff09\uff0c\u4f18\u5148\u7528<code>EXISTS<\/code>\uff08\u627e\u5230\u5339\u914d\u884c\u7acb\u5373\u505c\u6b62\uff09\u66ff\u4ee3<code>IN<\/code>\uff08\u9700\u904d\u5386\u5168\u91cf\u7ed3\u679c\u96c6\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>05. \u5185\u8fde\u63a5<\/h2>\n<p><code>INNER JOIN<\/code>\uff0c\u9ed8\u8ba4\u8fde\u63a5\u65b9\u5f0f\u3002<\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u4ec5\u4fdd\u7559\u4e24\u8868\u4e2d \u201c\u5173\u8054\u6761\u4ef6\u5b8c\u5168\u5339\u914d\u201d \u7684\u884c\uff08\u53d6\u4ea4\u96c6\uff09\uff0c<code>INNER<\/code>\u53ef\u7701\u7565\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u7528\u6237\u53ca\u5176\u6709\u6548\u8ba2\u5355\uff08\u4ec5\u8fd4\u56de\u6709\u8ba2\u5355\u7684\u7528\u6237\u548c\u5339\u914d\u7684\u8ba2\u5355\uff09\nSELECT u.username, o.order_no FROM user u\nINNER JOIN order o ON u.user_id = o.user_id  -- \u5173\u8054\u6761\u4ef6\uff08\u5fc5\u987b\u5199ON\uff09\uff0cINNER \u53ef\u7701\u7565\nWHERE o.status = 1;<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u4f4e\uff0cMySQL \u4f1a\u81ea\u52a8\u9009\u62e9 \u201c\u5c0f\u8868\u4f5c\u4e3a\u9a71\u52a8\u8868\u201d\uff0c\u82e5\u5173\u8054\u5b57\u6bb5\u6709\u7d22\u5f15\uff0c\u88ab\u9a71\u52a8\u8868\u53ef\u5feb\u901f\u5339\u914d\uff08\u907f\u514d\u5168\u8868\u626b\u63cf\uff09\uff1b<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u9700\u83b7\u53d6\u591a\u8868 \u201c\u4ea4\u96c6\u6570\u636e\u201d\uff08\u5982\u7528\u6237 + \u5173\u8054\u8ba2\u5355\u3001\u5546\u54c1 + \u5173\u8054\u5206\u7c7b\uff09\uff0c\u662f\u4e1a\u52a1\u4e2d\u6700\u5e38\u7528\u7684\u8fde\u63a5\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>06. \u5de6\u5916\u8fde\u63a5<\/h2>\n<p><code>LEFT JOIN<\/code> \/ <code>LEFT OUTER JOIN<\/code><\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u4fdd\u7559\u5de6\u8868\u6240\u6709\u884c\uff0c\u53f3\u8868\u65e0\u5339\u914d\u65f6\u7528<code>NULL<\/code>\u586b\u5145\uff0c<code>OUTER<\/code>\u53ef\u7701\u7565\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u7528\u6237\u53ca\u5176\u8ba2\u5355\uff08\u65e0\u8ba2\u5355\u7684\u7528\u6237\u4e5f\u4f1a\u663e\u793a\uff0c\u8ba2\u5355\u5b57\u6bb5\u4e3aNULL\uff09\nSELECT u.username, o.order_no FROM user u\nLEFT JOIN order o ON u.user_id = o.user_id\nWHERE o.status = 1 OR o.order_no IS NULL;  -- \u6ce8\u610f\uff1a\u8fc7\u6ee4\u53f3\u8868\u9700\u5305\u542bNULL<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u4e2d\u4f4e\uff0c\u9a71\u52a8\u8868\u662f\u5de6\u8868\uff08\u9700\u5168\u626b\u5de6\u8868\uff09\uff0c\u53f3\u8868\u5173\u8054\u5b57\u6bb5\u6709\u7d22\u5f15\u5219\u6027\u80fd\u63a5\u8fd1\u5185\u8fde\u63a5\uff1b\u65e0\u7d22\u5f15\u5219\u6027\u80fd\u4e0b\u964d\u660e\u663e\uff1b<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u9700\u4fdd\u7559\u4e3b\u8868\u5168\u91cf\u6570\u636e\uff08\u5982\u7528\u6237\u8868\uff09\uff0c\u5173\u8054\u4ece\u8868\uff08\u5982\u8ba2\u5355\u8868\uff09\u7684\u53ef\u9009\u6570\u636e\uff08\u5982 \u201c\u7edf\u8ba1\u6240\u6709\u7528\u6237\u7684\u8ba2\u5355\u60c5\u51b5\uff0c\u5305\u62ec\u65e0\u8ba2\u5355\u7528\u6237\u201d\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>07. \u53f3\u5916\u8fde\u63a5<\/h2>\n<p><code>RIGHT JOIN<\/code> \/ <code>RIGHT OUTER JOIN<\/code><\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u4fdd\u7559\u53f3\u8868\u6240\u6709\u884c\uff0c\u5de6\u8868\u65e0\u5339\u914d\u65f6\u7528<code>NULL<\/code>\u586b\u5145\uff0c\u903b\u8f91\u4e0e\u5de6\u8fde\u63a5\u76f8\u53cd\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u8ba2\u5355\u53ca\u5176\u6240\u5c5e\u7528\u6237\uff08\u65e0\u5339\u914d\u7528\u6237\u7684\u8ba2\u5355\u4e5f\u663e\u793a\uff0c\u7528\u6237\u5b57\u6bb5\u4e3aNULL\uff09\nSELECT u.username, o.order_no FROM user u\nRIGHT JOIN order o ON u.user_id = o.user_id\nWHERE o.create_time &gt; &#039;2024-01-01&#039;;<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u4e0e\u5de6\u8fde\u63a5\u4e00\u81f4\uff0c\u9a71\u52a8\u8868\u662f\u53f3\u8868\uff08\u9700\u5168\u626b\u53f3\u8868\uff09\uff1b<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u9700\u4fdd\u7559\u4ece\u8868\u5168\u91cf\u6570\u636e\uff08\u5982\u8ba2\u5355\u8868\uff09\uff0c\u5173\u8054\u4e3b\u8868\uff08\u5982\u7528\u6237\u8868\uff09\u7684\u53ef\u9009\u6570\u636e\uff08\u5982 \u201c\u7edf\u8ba1\u6240\u6709\u8fd1 3 \u4e2a\u6708\u8ba2\u5355\uff0c\u5305\u62ec\u5df2\u5220\u9664\u7528\u6237\u7684\u8ba2\u5355\u201d\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>08. \u5168\u5916\u8fde\u63a5<\/h2>\n<p><code>FULL JOIN<\/code> \/ <code>FULL OUTER JOIN<\/code><\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u4fdd\u7559\u5de6\u53f3\u4e24\u8868\u6240\u6709\u884c\uff0c\u65e0\u5339\u914d\u65f6\u7528<code>NULL<\/code>\u586b\u5145\uff1bMySQL \u4e0d\u76f4\u63a5\u652f\u6301<code>FULL JOIN<\/code>\uff0c\u9700\u7528<code>UNION<\/code>\u5408\u5e76\u5de6\u8fde\u63a5\u548c\u53f3\u8fde\u63a5\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u7528\u6237\u548c\u6240\u6709\u8ba2\u5355\u7684\u5173\u8054\u6570\u636e\uff08\u4fdd\u7559\u53cc\u65b9\u65e0\u5339\u914d\u7684\u884c\uff09\nSELECT u.username, o.order_no FROM user u\nLEFT JOIN order o ON u.user_id = o.user_id\nUNION  -- \u53bb\u91cd\u5408\u5e76\nSELECT u.username, o.order_no FROM user u\nRIGHT JOIN order o ON u.user_id = o.user_id;<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u9ad8\uff0c\u9700\u6267\u884c\u4e24\u6b21\u8fde\u63a5 + \u53bb\u91cd\u64cd\u4f5c\uff0c\u6570\u636e\u91cf\u5927\u65f6\u6548\u7387\u4f4e\uff1b<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u6781\u5c11\u7528\uff0c\u4ec5\u9700 \u201c\u5b8c\u6574\u4fdd\u7559\u4e24\u8868\u6240\u6709\u6570\u636e\u201d \u7684\u7279\u6b8a\u573a\u666f\uff08\u6570\u636e\u5bf9\u8d26\u3001\u5168\u91cf\u7edf\u8ba1\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>09. \u4ea4\u53c9\u8fde\u63a5<\/h2>\n<p><code>CROSS JOIN<\/code>\uff0c\u7b1b\u5361\u5c14\u79ef<\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u65e0\u5173\u8054\u6761\u4ef6\uff0c\u4e24\u8868\u6570\u636e\u5b8c\u5168\u7ec4\u5408\uff08\u884c\u6570 = \u5de6\u8868\u884c\u6570 \u00d7 \u53f3\u8868\u884c\u6570\uff09\uff0c<code>CROSS JOIN<\/code>\u53ef\u7701\u7565\uff1b\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u7528\u6237\u8868\uff083\u884c\uff09\u548c\u5546\u54c1\u8868\uff082\u884c\uff09\u4ea4\u53c9\u8fde\u63a5\uff0c\u7ed3\u679c\u4e3a6\u884c\nSELECT u.username, p.product_name FROM user u\nCROSS JOIN product p;<\/code><\/pre>\n<\/li>\n<li><strong>\u6027\u80fd\u5f00\u9500\uff1a<\/strong>\u6781\u9ad8\uff0c\u884c\u6570\u5448\u6307\u6570\u7ea7\u589e\u957f\uff081 \u4e07\u884c \u00d71 \u4e07\u884c = 1 \u4ebf\u884c\uff09\uff0c\u51e0\u4e4e\u4e0d\u7528\uff1b<\/li>\n<li><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u4ec5\u7528\u4e8e \u201c\u5168\u7ec4\u5408\u573a\u666f\u201d\uff08\u5982\u751f\u6210\u6240\u6709\u7528\u6237 + \u6240\u6709\u5546\u54c1\u7684\u6d4b\u8bd5\u6570\u636e\uff09\uff0c\u5fc5\u987b\u914d\u5408<code>WHERE<\/code>\u8fc7\u6ee4\uff08\u5426\u5219\u4f1a\u6491\u7206\u5185\u5b58\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>10. \u534a\u8fde\u63a5<\/h2>\n<p>\u534a\u8fde\u63a5\u4e0d\u662f\u72ec\u7acb\u7684\u67e5\u8be2\u8bed\u6cd5\uff0c\u800c\u662f MySQL 5.7 + \u9ed8\u8ba4\u542f\u7528\u7684\u5bf9<strong>\u5217\u5b50\u67e5\u8be2\uff08\u5982<code>IN<\/code>\u5b50\u67e5\u8be2\uff09\u7684\u5185\u90e8\u4f18\u5316\u624b\u6bb5<\/strong>\uff0c\u76ee\u7684\u662f\u51cf\u5c11\u5b50\u67e5\u8be2\u4e0e\u4e3b\u67e5\u8be2\u7684\u5339\u914d\u5f00\u9500\u3002<\/p>\n<p>\u7528\u4e8e\u4f18\u5316\u201c\u4e3b\u67e5\u8be2\u5339\u914d\u5b50\u67e5\u8be2\u7ed3\u679c\u96c6\u201d\u7684\u573a\u666f\uff08\u5982<code>IN<\/code>\/<code>EXISTS<\/code>\u5217\u5b50\u67e5\u8be2\uff09\u3002<\/p>\n<p>\u6838\u5fc3\u903b\u8f91\u662f \u201c\u53ea\u5224\u65ad\u5339\u914d\u4e0e\u5426\uff0c\u4e0d\u8fd4\u56de\u5b50\u67e5\u8be2\u7684\u5b8c\u6574\u6570\u636e\u201d\uff0c\u907f\u514d\u9010\u884c\u5339\u914d\u7684\u4f4e\u6548\u95ee\u9898\u3002<\/p>\n<p>\u7b80\u5355\u8bf4\uff1a\u666e\u901a\u5b50\u67e5\u8be2\u662f \u201c\u5b50\u67e5\u8be2\u8fd4\u56de\u6240\u6709\u7ed3\u679c\u2192\u4e3b\u67e5\u8be2\u9010\u884c\u6bd4\u5bf9\u201d\uff0c\u534a\u8fde\u63a5\u662f \u201c\u4e3b\u67e5\u8be2\u4e0e\u5b50\u67e5\u8be2\u8868\u76f4\u63a5\u5173\u8054\u2192\u627e\u5230\u5339\u914d\u884c\u5c31\u505c\u6b62\u201d\uff0c\u672c\u8d28\u662f\u5c06\u5b50\u67e5\u8be2\u8f6c\u5316\u4e3a\u7c7b\u4f3cJOIN\u7684\u5173\u8054\u903b\u8f91\uff0c\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p><strong>\u89e6\u53d1\u6761\u4ef6\uff1a<\/strong>\u5b50\u67e5\u8be2\u662f\u201c\u5217\u5b50\u67e5\u8be2\u201d\uff08\u8fd4\u56de\u5355\u5217\u591a\u884c\uff09\uff0c\u4e14\u5b50\u67e5\u8be2\u8868\u4e0e\u4e3b\u67e5\u8be2\u8868\u65e0\u5173\u8054\uff08\u975e\u5173\u8054\u5b50\u67e5\u8be2\uff09\uff0c\u4f8b\u5982<code>WHERE id IN (SELECT uid FROM t2 WHERE status=1)<\/code>\u3002<\/p>\n<p><strong>\u5199\u6cd5\u683c\u5f0f\uff1a<\/strong>\u65e0\u7279\u6b8a\u8bed\u6cd5\uff0c\u4ecd\u7528\u666e\u901a<code>IN<\/code>\/<code>EXISTS<\/code>\u5b50\u67e5\u8be2\u5199\u6cd5\uff0cMySQL \u4f1a\u81ea\u52a8\u89e6\u53d1\u534a\u8fde\u63a5\u4f18\u5316\uff08\u53ef\u901a\u8fc7<code>EXPLAIN<\/code>\u67e5\u770b<code>type<\/code>\u5217\u662f\u5426\u6709<code>SIMPLE<\/code>\/<code>HASH JOIN<\/code>\uff0c\u800c\u975e<code>SUBQUERY<\/code>\uff09\u3002<\/p>\n<pre><code class=\"language-sql\">-- \u793a\u4f8b\uff1a\u67e5\u8be2\u6709\u672a\u652f\u4ed8\u8ba2\u5355\u7684\u7528\u6237\uff08MySQL\u4f1a\u81ea\u52a8\u7528\u534a\u8fde\u63a5\u4f18\u5316\uff09\nSELECT username FROM user u WHERE u.user_id IN (SELECT DISTINCT o.user_id FROM order o WHERE o.status = 0);<\/code><\/pre>\n<p><strong>\u6027\u80fd\u5f00\u9500<\/strong><\/p>\n<ul>\n<li>\u4f4e &#8211; \u4e2d\uff1a\u8fdc\u4f18\u4e8e\u672a\u4f18\u5316\u7684\u5b50\u67e5\u8be2\uff08\u907f\u514d\u5b50\u67e5\u8be2\u7ed3\u679c\u96c6\u7269\u5316\u540e\u9010\u884c\u5339\u914d\uff09\u3002<\/li>\n<li>\u4f18\u5316\u903b\u8f91\uff1aMySQL \u4f1a\u9009\u62e9\u4ee5\u4e0b\u65b9\u5f0f\u4e4b\u4e00\u6267\u884c\u534a\u8fde\u63a5\uff1a\n<ul>\n<li>\u54c8\u5e0c\u534a\u8fde\u63a5\uff1a\u5bf9\u5c0f\u8868\uff08\u5b50\u67e5\u8be2\u8868\uff09\u5efa\u54c8\u5e0c\u8868\uff0c\u4e3b\u8868\u9010\u884c\u5339\u914d\u54c8\u5e0c\u952e\uff08\u9002\u5408\u5927\u8868\uff09\u3002<\/li>\n<li>\u5d4c\u5957\u5faa\u73af\u534a\u8fde\u63a5\uff1a\u5c0f\u8868\u9a71\u52a8\u5927\u8868\uff0c\u627e\u5230\u5339\u914d\u884c\u7acb\u5373\u505c\u6b62\u626b\u63cf\uff08\u9002\u5408\u5c0f\u8868\uff09\u3002<\/li>\n<li>\u7269\u5316\u534a\u8fde\u63a5\uff1a\u5b50\u67e5\u8be2\u7ed3\u679c\u5b58\u5165\u4e34\u65f6\u8868\u5e76\u5efa\u7d22\u5f15\uff0c\u4e3b\u8868\u5173\u8054\u4e34\u65f6\u8868\uff08\u9002\u5408\u5b50\u67e5\u8be2\u7ed3\u679c\u96c6\u8f83\u5c0f\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u4e3b\u67e5\u8be2\u9700\u5224\u65ad \u201c\u5b57\u6bb5\u662f\u5426\u5728\u5b50\u67e5\u8be2\u7ed3\u679c\u96c6\u4e2d\u201d\uff08\u5982<code>IN<\/code>\/<code>EXISTS<\/code>\uff09\u3002<\/li>\n<li>\u5b50\u67e5\u8be2\u8868\u6570\u636e\u91cf\u9002\u4e2d\uff081 \u4e07\u884c\u5185\u6700\u4f73\uff09\uff0c\u4e14\u5b50\u67e5\u8be2\u6709\u8fc7\u6ee4\u6761\u4ef6\uff08\u5982<code>WHERE status=1<\/code>\uff09\uff0c\u80fd\u51cf\u5c11\u7ed3\u679c\u96c6\u5927\u5c0f\u3002<\/li>\n<li>\u66ff\u4ee3\u573a\u666f\uff1a\u82e5\u5b50\u67e5\u8be2\u7ed3\u679c\u96c6\u6781\u5927\uff0810 \u4e07\u884c +\uff09\uff0c\u5efa\u8bae\u624b\u52a8\u6539\u5199\u4e3a<code>JOIN<\/code>\uff08\u534a\u8fde\u63a5\u4f18\u5316\u6548\u679c\u4f1a\u4e0b\u964d\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u534a\u8fde\u63a5\u662f MySQL \u81ea\u52a8\u4f18\u5316\uff0c\u65e0\u9700\u4e13\u95e8\u624b\u52a8\u53bb\u5199\u8bed\u6cd5\uff0c\u4f46\u9700\u77e5\u9053 \u201c<code>IN<\/code>\u5b50\u67e5\u8be2\u5728 5.7 + \u4f1a\u88ab\u4f18\u5316\u4e3a\u534a\u8fde\u63a5\u201d\uff0c\u907f\u514d\u9762\u8bd5\u5b98\u95ee \u201c<code>IN<\/code>\u548c<code>JOIN<\/code>\u54ea\u4e2a\u5feb\u201d \u65f6\u53ea\u8bf4\u8868\u9762\u7ed3\u8bba\u3002<\/li>\n<li>\u5b9e\u8df5\u4e2d\uff0c\u82e5<code>EXPLAIN<\/code>\u663e\u793a\u5b50\u67e5\u8be2\u7c7b\u578b\u4e3a<code>SUBQUERY<\/code>\uff08\u672a\u89e6\u53d1\u534a\u8fde\u63a5\uff09\uff0c\u53ef\u901a\u8fc7 \u201c\u5b50\u67e5\u8be2\u52a0<code>DISTINCT<\/code>\u201d \u6216 \u201c\u6539\u5199\u4e3a<code>JOIN<\/code>\u201d \u5f3a\u5236\u4f18\u5316\uff08\u5982\u4e0a\u8ff0\u793a\u4f8b\u52a0<code>DISTINCT<\/code>\u8ba9\u7ed3\u679c\u96c6\u552f\u4e00\uff0c\u66f4\u6613\u89e6\u53d1\u534a\u8fde\u63a5\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>\u603b\u7ed3<\/h2>\n<p><strong>1. \u5b50\u67e5\u8be2 vs \u8fde\u63a5\u67e5\u8be2\uff1a<\/strong><\/p>\n<ul>\n<li>\u7b80\u5355\u573a\u666f\uff08\u5982\u5355\u503c\u5339\u914d\u3001\u5b58\u5728\u6027\u6821\u9a8c\uff09\u7528\u5b50\u67e5\u8be2\uff08\u5199\u6cd5\u7b80\u6d01\uff09\uff1b<\/li>\n<li>\u590d\u6742\u591a\u8868\u5173\u8054\uff08\u5982 3 \u8868\u4ee5\u4e0a\u3001\u9700\u7b5b\u9009\u591a\u5b57\u6bb5\uff09\u7528\u8fde\u63a5\u67e5\u8be2\uff08\u6027\u80fd\u66f4\u4f18\uff0c\u6613\u4f18\u5316\uff09\uff1b<\/li>\n<li>\u5173\u8054\u5b50\u67e5\u8be2\u5c3d\u91cf\u6539\u5199\u4e3aJOIN\uff08\u907f\u514d\u5d4c\u5957\u5faa\u73af\uff0c\u5982<code>EXISTS<\/code>\u9002\u5408\u5c0f\u8868\uff0c<code>JOIN<\/code>\u9002\u5408\u5927\u8868\uff09\u3002<\/li>\n<\/ul>\n<p><strong>2. \u6027\u80fd\u4f18\u5316\u5173\u952e\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fde\u63a5\u67e5\u8be2\uff1a\u5173\u8054\u5b57\u6bb5\u5fc5\u987b\u5efa\u7d22\u5f15\uff08\u5982<code>order.user_id<\/code>\uff09\uff0c\u5c0f\u8868\u9a71\u52a8\u5927\u8868\uff08\u51cf\u5c11\u5faa\u73af\u6b21\u6570\uff09\uff1b<\/li>\n<li>\u5b50\u67e5\u8be2\uff1a\u907f\u514d<code>NOT IN<\/code>\uff08<code>NULL<\/code> \u503c\u4f1a\u5bfc\u81f4\u7ed3\u679c\u5f02\u5e38\uff09\uff0c\u7528<code>NOT EXISTS<\/code>\u66ff\u4ee3\uff1b<\/li>\n<li>\u5916\u8fde\u63a5\uff1a\u53f3\u8868\u8fc7\u6ee4\u6761\u4ef6\u5199\u5728<code>ON<\/code>\uff08\u5173\u8054\u65f6\u8fc7\u6ee4\uff09\uff0c\u5de6\u8868\u8fc7\u6ee4\u6761\u4ef6\u5199\u5728<code>WHERE<\/code>\uff08\u5173\u8054\u540e\u8fc7\u6ee4\uff09\u3002<\/li>\n<\/ul>\n<p><strong>3. Java \u5f00\u53d1\u5b9e\u8df5\u573a\u666f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5217\u8868\u67e5\u8be2\uff08\u5982\u7528\u6237\u5217\u8868 + \u8ba2\u5355\u6570\uff09\uff1a\u7528<code>LEFT JOIN<\/code> + <code>GROUP BY<\/code>\uff1b<\/li>\n<li>\u8be6\u60c5\u67e5\u8be2\uff08\u5982\u8ba2\u5355\u8be6\u60c5 + \u7528\u6237\u4fe1\u606f\uff09\uff1a\u7528<code>INNER JOIN<\/code>\uff1b<\/li>\n<li>\u5b58\u5728\u6027\u6821\u9a8c\uff08\u5982\u5224\u65ad\u7528\u6237\u662f\u5426\u6709\u672a\u652f\u4ed8\u8ba2\u5355\uff09\uff1a\u7528<code>EXISTS<\/code>\u5b50\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h1>02. \u5b50\u67e5\u8be2\u4e0e<code>join<\/code>\u6027\u80fd\u5bf9\u6bd4<\/h1>\n<p>\u5b50\u67e5\u8be2\u4e0e<code>join<\/code>\u6027\u80fd\u5bf9\u6bd4\u53ca\u9002\u7528\u573a\u666f<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png'><img class=\"lazyload lazyload-style-6\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p><strong>\u5173\u952e\u7ed3\u8bba\uff1a<\/strong><\/p>\n<ul>\n<li>\u7b80\u5355\u573a\u666f\uff08\u5355\u8868\u6821\u9a8c\u3001\u5355\u503c\u5339\u914d\uff09\u7528\u5b50\u67e5\u8be2\uff08\u7b80\u6d01\uff09\uff1b<\/li>\n<li>\u591a\u8868\u5173\u8054\u3001\u590d\u6742\u805a\u5408\u3001\u4e3b\u8868\u6570\u636e\u91cf\u5927\u65f6\uff0c\u4f18\u5148\u7528 JOIN\uff08\u6027\u80fd\u66f4\u7a33\u5b9a\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h1>03. \u590d\u6742\u67e5\u8be2\u7684\u6267\u884c\u903b\u8f91<\/h1>\n<h2>1. <code>group by<\/code>\/<code>having<\/code><\/h2>\n<p><code>GROUP BY<\/code> \u548c <code>HAVING<\/code> \u662f MySQL \u4e2d\u7528\u4e8e\u6570\u636e\u5206\u7ec4\u805a\u5408\u7684\u6838\u5fc3\u8bed\u6cd5\uff0c\u6267\u884c\u903b\u8f91\uff1a \u201c\u6570\u636e\u8fc7\u6ee4\u2192\u5206\u7ec4\u2192\u805a\u5408\u2192\u4e8c\u6b21\u8fc7\u6ee4\u201d \u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>1. \u6838\u5fc3\u6267\u884c\u903b\u8f91<\/h3>\n<p><code>GROUP BY<\/code> \u7684\u4f5c\u7528\u662f \u201c\u6309\u6307\u5b9a\u5b57\u6bb5\u5c06\u6570\u636e\u5206\u7ec4\u201d\uff1b<br \/>\n<code>HAVING<\/code> \u5219\u662f \u201c\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u201d\u3002<\/p>\n<div style=\"height:0.7em\"><\/div>\n<p>\u6574\u4f53\u6267\u884c\u6d41\u7a0b\u5206\u56db\u6b65\uff0c\u987a\u5e8f\u4e0d\u53ef\u98a0\u5012\uff1a<\/p>\n<p><strong>1) \u539f\u59cb\u6570\u636e\u8fc7\u6ee4\uff08<code>WHERE<\/code> \u5b50\u53e5\uff09\uff1b<\/strong><\/p>\n<ul>\n<li>\u5148\u901a\u8fc7 <code>WHERE<\/code> \u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u539f\u59cb\u6570\u636e\uff08\u5982 <code>WHERE status = 1<\/code> \u8fc7\u6ee4\u65e0\u6548\u6570\u636e\uff09\uff0c\u51cf\u5c11\u540e\u7eed\u5904\u7406\u7684\u6570\u636e\u91cf\u3002<\/li>\n<li>\u539f\u7406\uff1a<code>WHERE<\/code> \u662f \u201c\u5206\u7ec4\u524d\u8fc7\u6ee4\u201d\uff0c\u4ec5\u4fdd\u7559\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\uff0c\u4e0d\u6d89\u53ca\u805a\u5408\u64cd\u4f5c\uff0c\u53ef\u5229\u7528\u7d22\u5f15\u5feb\u901f\u7b5b\u9009\uff08\u5982 <code>status<\/code> \u5b57\u6bb5\u6709\u7d22\u5f15\u65f6\uff0c\u76f4\u63a5\u5b9a\u4f4d\u6709\u6548\u884c\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>2) \u5206\u7ec4\u64cd\u4f5c\uff08<code>GROUP BY<\/code> \u5b50\u53e5\uff09\uff1b<\/strong><\/p>\n<ul>\n<li>\u6309 <code>GROUP BY<\/code> \u540e\u7684\u5b57\u6bb5\uff08\u5982 <code>GROUP BY user_id<\/code>\uff09\u5c06\u4e0a\u4e00\u6b65\u8fc7\u6ee4\u540e\u7684\u6570\u636e\u5206\u7ec4\uff0c\u76f8\u540c\u503c\u7684\u884c\u88ab\u5f52\u4e3a\u4e00\u7ec4\u3002<\/li>\n<li>\u5e95\u5c42\u5904\u7406\uff1a\n<ul>\n<li>\u82e5\u5206\u7ec4\u5b57\u6bb5\u6709\u7d22\u5f15\uff0cMySQL \u4f1a\u76f4\u63a5\u6309\u7d22\u5f15\u987a\u5e8f\u5206\u7ec4\uff08\u65e0\u9700\u989d\u5916\u6392\u5e8f\uff0c\u6548\u7387\u9ad8\uff09\uff1b<\/li>\n<li>\u82e5\u65e0\u7d22\u5f15\uff0c\u4f1a\u521b\u5efa\u4e34\u65f6\u8868\u5b58\u50a8\u5206\u7ec4\u6570\u636e\uff0c\u6216\u5bf9\u6570\u636e\u8fdb\u884c\u6587\u4ef6\u6392\u5e8f\uff08<code>Using temporary<\/code> \u6216 <code>Using filesort<\/code>\uff0c\u53ef\u901a\u8fc7 <code>EXPLAIN<\/code> \u67e5\u770b\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>3) \u805a\u5408\u8ba1\u7b97\uff08\u805a\u5408\u51fd\u6570\uff09\uff1b<\/strong><\/p>\n<ul>\n<li>\u5bf9\u6bcf\u4e2a\u5206\u7ec4\u6267\u884c\u805a\u5408\u51fd\u6570\uff08\u5982 <code>COUNT(*)<\/code> \u7edf\u8ba1\u884c\u6570\u3001<code>SUM(amount)<\/code> \u8ba1\u7b97\u603b\u548c\uff09\uff0c\u751f\u6210\u6bcf\u4e2a\u5206\u7ec4\u7684\u805a\u5408\u7ed3\u679c\u3002<\/li>\n<li>\u539f\u7406\uff1a\u805a\u5408\u51fd\u6570\u4ec5\u4f5c\u7528\u4e8e\u5206\u7ec4\u5185\u7684\u6570\u636e\uff0c\u6bcf\u4e2a\u5206\u7ec4\u6700\u7ec8\u8f93\u51fa\u4e00\u884c\u7ed3\u679c\uff08\u5305\u542b\u5206\u7ec4\u5b57\u6bb5\u548c\u805a\u5408\u503c\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>4) \u5206\u7ec4\u7ed3\u679c\u8fc7\u6ee4\uff08<code>HAVING<\/code> \u5b50\u53e5\uff09\uff1b<\/strong><\/p>\n<ul>\n<li>\u7528 <code>HAVING<\/code> \u7b5b\u9009\u805a\u5408\u540e\u7684\u5206\u7ec4\uff08\u5982 <code>HAVING COUNT(*) &gt; 5<\/code> \u4fdd\u7559\u884c\u6570\u8d85 5 \u7684\u7ec4\uff09\uff0c\u6700\u7ec8\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u5206\u7ec4\u3002<\/li>\n<li>\u539f\u7406\uff1a<code>HAVING<\/code> \u662f \u201c\u5206\u7ec4\u540e\u8fc7\u6ee4\u201d\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u805a\u5408\u51fd\u6570\uff08\u56e0\u805a\u5408\u7ed3\u679c\u5df2\u751f\u6210\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h3>2. \u5173\u952e\u533a\u522b\u4e0e\u5b9e\u8df5\u6ce8\u610f<\/h3>\n<p><strong>1) WHERE \u4e0e HAVING \u7684\u6838\u5fc3\u533a\u522b<\/strong><\/p>\n<ul>\n<li><code>WHERE<\/code> \u4f5c\u7528\u4e8e\u5206\u7ec4\u524d\u7684\u539f\u59cb\u6570\u636e\uff0c\u4e0d\u80fd\u4f7f\u7528\u805a\u5408\u51fd\u6570\uff08\u5982 <code>WHERE COUNT(*) &gt; 5<\/code> \u4f1a\u62a5\u9519\uff09\uff1b<\/li>\n<li><code>HAVING<\/code> \u4f5c\u7528\u4e8e\u5206\u7ec4\u540e\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u805a\u5408\u51fd\u6570\uff08\u5982 <code>HAVING SUM(amount) &gt; 1000<\/code> \u5408\u6cd5\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>2) \u6027\u80fd\u4f18\u5316\u5b9e\u8df5\uff08\u91cd\u70b9\uff09<\/strong><\/p>\n<ul>\n<li><strong>\u4f18\u5148\u7528 <code>WHERE<\/code> \u8fc7\u6ee4\uff1a<\/strong>\u5c3d\u91cf\u5728\u5206\u7ec4\u524d\u901a\u8fc7 <code>WHERE<\/code> \u51cf\u5c11\u6570\u636e\u91cf\uff08\u5982 <code>GROUP BY user_id HAVING user_id &gt; 100<\/code> \u53ef\u6539\u4e3a <code>WHERE user_id &gt; 100 GROUP BY user_id<\/code>\uff0c\u51cf\u5c11\u5206\u7ec4\u8ba1\u7b97\u91cf\uff09\u3002<\/li>\n<li><strong>\u5206\u7ec4\u5b57\u6bb5\u52a0\u7d22\u5f15\uff1a<\/strong>\u907f\u514d\u4e34\u65f6\u8868\u548c\u6587\u4ef6\u6392\u5e8f\uff08\u5982 <code>GROUP BY create_time<\/code> \u65f6\uff0c\u7ed9 <code>create_time<\/code> \u5efa\u7d22\u5f15\uff0c<code>EXPLAIN<\/code> \u4e2d\u65e0 <code>Using temporary<\/code> \u5373\u4e3a\u4f18\u5316\u751f\u6548\uff09\u3002<\/li>\n<li><strong>\u907f\u514d <code>SELECT<\/code> \u975e\u5206\u7ec4\u5b57\u6bb5\uff1a<\/strong>MySQL 5.7+ \u9ed8\u8ba4\u5f00\u542f <code>ONLY_FULL_GROUP_BY<\/code> \u6a21\u5f0f\uff0c<code>SELECT<\/code> \u540e\u53ea\u80fd\u51fa\u73b0 <code>GROUP BY<\/code> \u5b57\u6bb5\u6216\u805a\u5408\u51fd\u6570\uff08\u5982 <code>SELECT user_id, username, COUNT(*)<\/code> \u82e5 <code>username<\/code> \u672a\u5728 <code>GROUP BY<\/code> \u4e2d\uff0c\u4f1a\u62a5\u9519\uff0c\u9700\u89c4\u8303\u5199\u6cd5\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h3>3. \u5178\u578b\u573a\u666f\u4e3e\u4f8b<\/h3>\n<ul>\n<li><strong>\u7edf\u8ba1\u9ad8\u9891\u7528\u6237\uff1a<\/strong>\u67e5\u8be2 \u201c\u8ba2\u5355\u6570\u8d85 10 \u5355\u7684\u7528\u6237\u53ca\u5176\u603b\u6d88\u8d39\u201d\uff0c\u7528 <code>GROUP BY user_id HAVING COUNT(*) &gt; 10<\/code>\u3002<\/li>\n<li><strong>\u6309\u65f6\u95f4\u5206\u7ec4\u5206\u6790\uff1a<\/strong>\u67e5\u8be2 \u201c\u6bcf\u6708\u8ba2\u5355\u91d1\u989d\u8d85 10 \u4e07\u7684\u6708\u4efd\u201d\uff0c\u7528 <code>GROUP BY month(create_time) HAVING SUM(amount) &gt; 100000<\/code>\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h2>2. <code>limit<\/code> \u5206\u9875<\/h2>\n<p><code>LIMIT<\/code> \u5206\u9875\u662f MySQL \u4e2d\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u622a\u53d6\u6307\u5b9a\u8303\u56f4\u6570\u636e\u7684\u6838\u5fc3\u8bed\u6cd5\uff08\u5982 <code>LIMIT offset, size<\/code> \u53d6\u7b2c <code>offset + 1<\/code> \u5230 <code>offset + size<\/code> \u884c\uff09\uff0c\u5176\u6267\u884c\u903b\u8f91\u56f4\u7ed5 \u201c\u5168\u91cf\u67e5\u8be2\u2192\u6392\u5e8f\u2192\u622a\u53d6\u201d \u5c55\u5f00\uff0c\u5b9e\u9645\u4f7f\u7528\u4e2d\u9700\u91cd\u70b9\u5173\u6ce8\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>1. \u57fa\u672c\u6267\u884c\u903b\u8f91<\/h3>\n<p><code>LIMIT<\/code> \u672c\u8eab\u4e0d\u53c2\u4e0e\u6570\u636e\u7b5b\u9009\uff0c\u800c\u662f\u5bf9 \u201c\u67e5\u8be2 + \u6392\u5e8f\u540e\u201d \u7684\u7ed3\u679c\u96c6\u8fdb\u884c\u622a\u53d6\uff0c\u6838\u5fc3\u6d41\u7a0b\u5206\u4e09\u6b65\uff1a<\/p>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>1) \u6267\u884c\u4e3b\u67e5\u8be2\u83b7\u53d6\u539f\u59cb\u6570\u636e<\/strong><\/p>\n<ul>\n<li>\u5148\u6267\u884c <code>WHERE<\/code>\u3001<code>JOIN<\/code> \u7b49\u6761\u4ef6\u7b5b\u9009\uff0c\u5f97\u5230\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u884c\uff08\u5982 <code>SELECT * FROM order WHERE status=1<\/code> \u7b5b\u9009\u6709\u6548\u8ba2\u5355\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>2) \u6392\u5e8f\u5904\u7406\uff08\u82e5\u6709 <code>ORDER BY<\/code>\uff09<\/strong><\/p>\n<ul>\n<li>\u82e5\u5305\u542b <code>ORDER BY<\/code>\uff08\u5206\u9875\u51e0\u4e4e\u5fc5\u5e26\uff0c\u5426\u5219\u7ed3\u679c\u65e0\u5e8f\uff09\uff0cMySQL \u4f1a\u6309\u6307\u5b9a\u5b57\u6bb5\u6392\u5e8f\uff1a\n<ul>\n<li>\u82e5\u6392\u5e8f\u5b57\u6bb5\u6709\u7d22\u5f15\uff08\u5982 <code>ORDER BY create_time<\/code> \u4e14 <code>create_time<\/code> \u6709\u7d22\u5f15\uff09\uff0c\u76f4\u63a5\u5229\u7528\u7d22\u5f15\u987a\u5e8f\u83b7\u53d6\u6709\u5e8f\u6570\u636e\uff08\u6548\u7387\u9ad8\uff09\uff1b<\/li>\n<li>\u82e5\u65e0\u7d22\u5f15\uff0c\u9700\u5728\u5185\u5b58\u6216\u78c1\u76d8\u4e2d\u8fdb\u884c \u201c\u6587\u4ef6\u6392\u5e8f\u201d\uff08<code>Using filesort<\/code>\uff0c\u53ef\u901a\u8fc7 <code>EXPLAIN<\/code> \u67e5\u770b\uff09\uff0c\u6392\u5e8f\u8fc7\u7a0b\u4f1a\u6d88\u8017\u989d\u5916 CPU\/IO\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>3) \u622a\u53d6\u6307\u5b9a\u8303\u56f4\u6570\u636e<\/strong><\/p>\n<ul>\n<li>\u4ece\u6392\u5e8f\u540e\u7684\u5b8c\u6574\u7ed3\u679c\u96c6\u4e2d\uff0c\u8df3\u8fc7\u524d offset \u884c\uff0c\u8fd4\u56de\u63a5\u4e0b\u6765\u7684 size \u884c\uff08\u5982 LIMIT 20, 10 \u5373\u8df3\u8fc7\u524d 20 \u884c\uff0c\u8fd4\u56de 10 \u884c\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h3>2. \u6838\u5fc3\u6027\u80fd\u95ee\u9898\uff1a<code>offset<\/code> \u8fc7\u5927\u5bfc\u81f4\u6548\u7387\u4f4e\u4e0b<\/h3>\n<p>\u5f53 <code>offset<\/code> \u5f88\u5927\u65f6\uff08\u5982 <code>LIMIT 100000, 10<\/code>\uff09\uff0c\u6027\u80fd\u4f1a\u6025\u5267\u4e0b\u964d\uff0c\u539f\u56e0\u662f\uff1a<\/p>\n<ul>\n<li>MySQL \u5fc5\u987b\u5148\u626b\u63cf\u5e76\u6392\u5e8f\u524d 100010 \u884c\u6570\u636e\uff0c\u7136\u540e\u4e22\u5f03\u524d 100000 \u884c\uff0c\u4ec5\u8fd4\u56de\u6700\u540e 10 \u884c\uff0c\u5927\u91cf\u8ba1\u7b97\u88ab\u6d6a\u8d39\uff1b<\/li>\n<li>\u82e5\u6392\u5e8f\u65e0\u7d22\u5f15\uff08\u4f9d\u8d56\u6587\u4ef6\u6392\u5e8f\uff09\uff0c\u4f1a\u4ea7\u751f\u4e34\u65f6\u6587\u4ef6\uff0c\u8fdb\u4e00\u6b65\u52a0\u5267\u6027\u80fd\u635f\u8017\uff0810 \u4e07\u884c\u6570\u636e\u6392\u5e8f\u53ef\u80fd\u8017\u65f6\u6570\u767e\u6beb\u79d2\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h3>3. \u5b9e\u8df5\u4f18\u5316\u65b9\u6848\uff08\u91cd\u70b9\uff0c\u4f53\u73b0\u5de5\u7a0b\u7ecf\u9a8c\uff09<\/h3>\n<p>\u9488\u5bf9 <code>offset<\/code> \u8fc7\u5927\u7684\u95ee\u9898\uff0c\u7ed3\u5408 Java \u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u5206\u9875\u573a\u666f\uff08\u5982\u5217\u8868\u9875\u3001\u5386\u53f2\u8bb0\u5f55\u67e5\u8be2\uff09\uff0c\u4f18\u5316\u624b\u6bb5\u5982\u4e0b\uff1a<\/p>\n<p><strong>1) \u57fa\u4e8e \u201c\u4e3b\u952e \/ \u552f\u4e00\u7d22\u5f15\u201d \u5206\u9875\uff08\u6700\u5e38\u7528\uff09<\/strong><\/p>\n<p>\u5229\u7528\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7 <code>WHERE<\/code> \u6761\u4ef6\u76f4\u63a5\u5b9a\u4f4d\u8d77\u59cb\u4f4d\u7f6e\uff0c\u907f\u514d\u5168\u91cf\u626b\u63cf\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u4ee3\u66ff LIMIT 100000, 10\uff08\u4f4e\u6548\uff09\nSELECT * FROM order \nWHERE id &gt; 100000  -- \u4e0a\u4e00\u9875\u6700\u540e\u4e00\u6761\u6570\u636e\u7684id\nORDER BY id \nLIMIT 10;<\/code><\/pre>\n<p>\u539f\u7406\uff1a\u4e3b\u952e\u7d22\u5f15\u662f\u6709\u5e8f\u7684\uff0cid &gt; 100000 \u53ef\u76f4\u63a5\u5b9a\u4f4d\u5230\u8d77\u59cb\u884c\uff0c\u65e0\u9700\u626b\u63cf\u524d 10 \u4e07\u884c\uff0c\u6027\u80fd\u63d0\u5347 10 \u500d\u4ee5\u4e0a\u3002<\/p>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>2) \u907f\u514d <code>SELECT *<\/code>\uff0c\u53ea\u67e5\u5fc5\u8981\u5b57\u6bb5<\/strong><\/p>\n<p>\u51cf\u5c11\u6570\u636e\u4f20\u8f93\u91cf\u548c\u5185\u5b58\u5360\u7528\uff0c\u5c24\u5176\u5927\u8868\uff08\u5982\u5305\u542b text \u5b57\u6bb5\u7684\u8868\uff09\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u53ea\u67e5\u9700\u8981\u7684\u5b57\u6bb5\uff08\u5982\u8ba2\u5355\u53f7\u3001\u91d1\u989d\u3001\u65f6\u95f4\uff09\nSELECT order_no, amount, create_time FROM order \nWHERE id &gt; 100000 \nLIMIT 10;<\/code><\/pre>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>3) \u786e\u4fdd\u6392\u5e8f\u5b57\u6bb5\u6709\u7d22\u5f15<\/strong><\/p>\n<p>\u5206\u9875\u5fc5\u987b\u5e26 <code>ORDER BY<\/code>\uff0c\u4e14\u6392\u5e8f\u5b57\u6bb5\u9700\u5efa\u7d22\u5f15\uff08\u5982 <code>create_time<\/code> \u7d22\u5f15\uff09\uff0c\u907f\u514d\u6587\u4ef6\u6392\u5e8f\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u7ed9 create_time \u5efa\u7d22\u5f15\uff1aCREATE INDEX idx_order_create_time ON order(create_time)\nSELECT * FROM order \nWHERE status=1 \nORDER BY create_time DESC  -- \u5229\u7528\u7d22\u5f15\u6392\u5e8f\nLIMIT 20, 10;<\/code><\/pre>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>4) \u9650\u5236\u6700\u5927\u5206\u9875\u9875\u6570<\/strong><\/p>\n<p>\u4e1a\u52a1\u4e0a\u907f\u514d\u5141\u8bb8\u7528\u6237\u8bbf\u95ee\u8fc7\u5927\u9875\u6570\uff08\u5982 \u201c\u53ea\u663e\u793a\u524d 100 \u9875\u201d\uff09\uff0c\u8d85\u8fc7\u5219\u63d0\u793a \u201c\u6570\u636e\u8fc7\u591a\uff0c\u8bf7\u7f29\u5c0f\u8303\u56f4\u201d\uff08\u5982\u7535\u5546\u5e73\u53f0\u5e38\u89c1\u505a\u6cd5\uff09\uff0c\u4ece\u6e90\u5934\u51cf\u5c11\u5927 <code>offset<\/code> \u573a\u666f\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h1>04. <code>order by<\/code> \u7684\u6392\u5e8f\u539f\u7406<\/h1>\n<p>MySQL \u7684<code>ORDER BY<\/code>\u7528\u4e8e\u5bf9\u67e5\u8be2\u7ed3\u679c\u6309\u6307\u5b9a\u5b57\u6bb5\u6392\u5e8f\uff0c\u5176\u6838\u5fc3\u539f\u7406\u662f \u201c<strong>\u5229\u7528\u7d22\u5f15\u6709\u5e8f\u6027\u76f4\u63a5\u53d6\u6570<\/strong>\u201d \u6216 \u201c<strong>\u65e0\u7d22\u5f15\u65f6\u901a\u8fc7\u5185\u5b58 \/ \u78c1\u76d8\u6392\u5e8f<\/strong>\u201d\uff0c\u6027\u80fd\u5dee\u5f02\u4e3b\u8981\u6e90\u4e8e\u662f\u5426\u80fd\u501f\u52a9\u7d22\u5f15\u907f\u514d\u989d\u5916\u6392\u5e8f\u64cd\u4f5c\u3002<\/p>\n<h2>1. \u6838\u5fc3\u6392\u5e8f\u539f\u7406<\/h2>\n<h3>1. \u5229\u7528\u7d22\u5f15\u6392\u5e8f<\/h3>\n<p><code>Using index<\/code>\uff0c\u9ad8\u6548\u3002<\/p>\n<p>\u82e5\u6392\u5e8f\u5b57\u6bb5\uff08\u6216\u591a\u5b57\u6bb5\u6392\u5e8f\u7684\u524d\u7f00\uff09\u6709<strong>\u6709\u5e8f\u7d22\u5f15<\/strong>\uff08\u5982 B + \u6811\u7d22\u5f15\uff0c\u672c\u8eab\u6309\u5b57\u6bb5\u503c\u6709\u5e8f\u5b58\u50a8\uff09\uff0cMySQL \u4f1a\u76f4\u63a5\u6cbf\u7d22\u5f15\u987a\u5e8f\u8bfb\u53d6\u6570\u636e\uff0c\u65e0\u9700\u989d\u5916\u6392\u5e8f\uff0c\u8fd9\u662f\u6700\u4f18\u60c5\u51b5\u3002<\/p>\n<ul>\n<li><strong>\u539f\u7406\uff1a<\/strong>B + \u6811\u7d22\u5f15\u7684\u53f6\u5b50\u8282\u70b9\u6309\u7d22\u5f15\u5b57\u6bb5\u503c\u5347\u5e8f\uff08\u6216\u964d\u5e8f\uff0c\u53d6\u51b3\u4e8e\u5efa\u7d22\u5f15\u65f6\u7684\u6307\u5b9a\uff09\u6392\u5217\uff0c<code>ORDER BY<\/code>\u5b57\u6bb5\u4e0e\u7d22\u5f15\u987a\u5e8f\u4e00\u81f4\u65f6\uff0c\u53ef\u76f4\u63a5\u901a\u8fc7\u7d22\u5f15\u5b9a\u4f4d\u5e76\u8fd4\u56de\u6709\u5e8f\u6570\u636e\uff0c\u907f\u514d\u6392\u5e8f\u5f00\u9500\u3002<\/li>\n<li><strong>\u793a\u4f8b\uff1a<\/strong><br \/>\n\u8ba2\u5355\u8868<code>order<\/code>\u6709\u7d22\u5f15<code>idx_create_time<\/code>\uff08<code>create_time<\/code>\u5347\u5e8f\uff09\uff0c\u6267\u884c<code>SELECT id, create_time FROM order ORDER BY create_time<\/code>\u65f6\uff0cMySQL \u76f4\u63a5\u6cbf<code>idx_create_time<\/code>\u7684\u53f6\u5b50\u8282\u70b9\u987a\u5e8f\u53d6\u6570\uff0c\u65e0\u9700\u6392\u5e8f\uff08<code>EXPLAIN<\/code>\u4e2d<code>Extra<\/code>\u5217\u663e\u793a<code>Using index<\/code>\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h3>2. \u6587\u4ef6\u6392\u5e8f<\/h3>\n<p><code>Using filesort<\/code>\uff0c\u4f4e\u6548\u3002<\/p>\n<p>\u82e5\u6392\u5e8f\u5b57\u6bb5\u65e0\u7d22\u5f15\uff0c\u6216\u6392\u5e8f\u987a\u5e8f\u4e0e\u7d22\u5f15\u987a\u5e8f\u4e0d\u4e00\u81f4\uff08\u5982\u7d22\u5f15\u662f\u5347\u5e8f\uff0c<code>ORDER BY<\/code>\u7528\u964d\u5e8f\u4e14\u65e0\u5bf9\u5e94\u964d\u5e8f\u7d22\u5f15\uff09\uff0cMySQL \u4f1a\u89e6\u53d1 \u201c\u6587\u4ef6\u6392\u5e8f\u201d\uff1a\u5148\u5c06\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58 \/ \u4e34\u65f6\u6587\u4ef6\uff0c\u5b8c\u6210\u6392\u5e8f\u540e\u518d\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u6267\u884c\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\u4ece\u8868\u4e2d\u8bfb\u53d6\u7b26\u5408<code>WHERE<\/code>\u6761\u4ef6\u7684\u884c\uff08\u65e0\u7d22\u5f15\u65f6\u5168\u8868\u626b\u63cf\uff09\uff1b<\/li>\n<li>\u5c06 \u201c\u6392\u5e8f\u5b57\u6bb5\u503c + \u884c\u6307\u9488\uff08\u6307\u5411\u539f\u59cb\u6570\u636e\u4f4d\u7f6e\uff09\u201d \u5b58\u5165<code>sort_buffer<\/code>\uff08\u6392\u5e8f\u7f13\u51b2\u533a\uff09\uff1b<\/li>\n<li>\u82e5<code>sort_buffer<\/code>\u88c5\u4e0d\u4e0b\u6240\u6709\u6570\u636e\uff0c\u4f1a\u5c06\u6570\u636e\u5206\u5757\uff0c\u5148\u5728\u5185\u5b58\u4e2d\u6392\u5e8f\uff0c\u518d\u5199\u5165\u4e34\u65f6\u6587\u4ef6\uff08\u78c1\u76d8\uff09\uff1b<\/li>\n<li>\u6700\u540e\u5408\u5e76\u6240\u6709\u5206\u5757\u7684\u6392\u5e8f\u7ed3\u679c\uff0c\u5f97\u5230\u5168\u5c40\u6709\u5e8f\u7684\u7ed3\u679c\u96c6\uff0c\u518d\u901a\u8fc7\u884c\u6307\u9488\u53d6\u539f\u59cb\u6570\u636e\u8fd4\u56de\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><br \/>\n\u8ba2\u5355\u8868<code>order<\/code>\u7684<code>create_time<\/code>\u65e0\u7d22\u5f15\uff0c\u6267\u884c<code>SELECT * FROM order ORDER BY create_time<\/code>\u65f6\uff0cMySQL \u4f1a\u5148\u626b\u63cf\u5168\u8868\uff0c\u5c06<code>create_time<\/code>\u548c\u884c\u6307\u9488\u5b58\u5165<code>sort_buffer<\/code>\uff0c\u6392\u5e8f\u540e\u518d\u53d6\u5b8c\u6574\u6570\u636e\uff08<code>EXPLAIN<\/code>\u4e2d<code>Extra<\/code>\u5217\u663e\u793a<code>Using filesort<\/code>\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>2. \u5f71\u54cd\u6392\u5e8f\u6027\u80fd\u7684\u5173\u952e\u56e0\u7d20<\/h2>\n<ol>\n<li><strong>\u662f\u5426\u4f7f\u7528\u7d22\u5f15<\/strong>\uff1a\u7d22\u5f15\u6392\u5e8f\u6027\u80fd\u8fdc\u9ad8\u4e8e\u6587\u4ef6\u6392\u5e8f\uff08\u6beb\u79d2\u7ea7 vs \u79d2\u7ea7\uff0c\u6570\u636e\u91cf\u5927\u65f6\u5dee\u8ddd\u66f4\u5927\uff09\u3002<\/li>\n<li><strong><code>sort_buffer<\/code>\u5927\u5c0f<\/strong>\uff1a\u7531<code>sort_buffer_size<\/code>\u53c2\u6570\u63a7\u5236\uff08\u9ed8\u8ba4 256KB\uff09\uff0c\u82e5\u6570\u636e\u91cf\u8d85\u8fc7\u8be5\u503c\uff0c\u4f1a\u89e6\u53d1\u78c1\u76d8\u4e34\u65f6\u6587\u4ef6\u6392\u5e8f\uff08IO \u5f00\u9500\u5267\u589e\uff09\u3002<\/li>\n<li><strong>\u67e5\u8be2\u5b57\u6bb5\u591a\u5c11<\/strong>\uff1a<code>SELECT *<\/code>\u4f1a\u5bfc\u81f4<code>sort_buffer<\/code>\u4e2d\u5b58\u50a8\u66f4\u591a\u5b57\u6bb5\uff08\u589e\u52a0\u5185\u5b58\u5360\u7528\uff09\uff0c\u800c\u53ea\u67e5\u5fc5\u8981\u5b57\u6bb5\u53ef\u51cf\u5c11<code>sort_buffer<\/code>\u538b\u529b\uff0c\u52a0\u901f\u6392\u5e8f\u3002<\/li>\n<\/ol>\n<div style=\"height:1em\"><\/div>\n<h2>3. \u5b9e\u8df5\u4f18\u5316\u5efa\u8bae<\/h2>\n<p><strong>1. \u7ed9\u6392\u5e8f\u5b57\u6bb5\u5efa\u5408\u9002\u7684\u7d22\u5f15\uff1a<\/strong><\/p>\n<ul>\n<li>\u5355\u5b57\u6bb5\u6392\u5e8f\uff1a\u76f4\u63a5\u7ed9\u6392\u5e8f\u5b57\u6bb5\u5efa\u7d22\u5f15\uff08\u5982<code>ORDER BY create_time<\/code> \u2192 \u5efa<code>idx_create_time<\/code>\uff09\u3002<\/li>\n<li>\u591a\u5b57\u6bb5\u6392\u5e8f\uff08\u5982<code>ORDER BY a, b DESC<\/code>\uff09\uff1a\u5efa\u8054\u5408\u7d22\u5f15(<code>a, b DESC<\/code>)\uff0c\u7d22\u5f15\u987a\u5e8f\u4e0e\u6392\u5e8f\u987a\u5e8f\u5b8c\u5168\u4e00\u81f4\uff08\u907f\u514d\u7d22\u5f15\u5931\u6548\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>2. \u907f\u514d<code>SELECT *<\/code>\uff0c\u53ea\u67e5\u5fc5\u8981\u5b57\u6bb5\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f8b\u5982\uff0c\u5217\u8868\u9875\u53ea\u9700<code>id<\/code>\u3001<code>name<\/code>\u3001<code>create_time<\/code>\uff0c\u5219<code>SELECT id, name, create_time ...<\/code>\uff0c\u51cf\u5c11<code>sort_buffer<\/code>\u4e2d\u5b58\u50a8\u7684\u6570\u636e\u91cf\uff0c\u907f\u514d\u78c1\u76d8\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>3. \u63a7\u5236\u6392\u5e8f\u6570\u636e\u91cf\uff1a<\/strong><br \/>\n\u5148\u7528<code>WHERE<\/code>\u8fc7\u6ee4\u65e0\u6548\u6570\u636e\uff08\u5982<code>WHERE status=1<\/code>\uff09\uff0c\u51cf\u5c11\u8fdb\u5165\u6392\u5e8f\u9636\u6bb5\u7684\u884c\u6570\uff08\u5982\u4ece 100 \u4e07\u884c\u6ee4\u5230 1 \u4e07\u884c\uff0c\u6392\u5e8f\u6548\u7387\u63d0\u5347 100 \u500d\uff09\u3002<\/p>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>4. \u5927\u7ed3\u679c\u96c6\u6392\u5e8f\u7528\u5206\u9875\u914d\u5408\u7d22\u5f15\uff1a<\/strong><br \/>\n\u5982 \u201c\u6309\u65f6\u95f4\u6392\u5e8f\u5206\u9875\u67e5\u8be2\u8ba2\u5355\u201d\uff0c\u5efa(<code>create_time, id<\/code>)\u8054\u5408\u7d22\u5f15\uff0c\u7528<code>WHERE create_time &gt; ?<\/code>\u5b9a\u4f4d\u5206\u9875\u8d77\u70b9\uff0c\u907f\u514d\u5168\u91cf\u6392\u5e8f\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>4. \u603b\u7ed3<\/h2>\n<p><code>ORDER BY<\/code>\u7684\u4f18\u5316\u6838\u5fc3\u662f \u201c<strong>\u80fd\u7528\u4e0a\u7d22\u5f15\u5c31\u907f\u514d\u6587\u4ef6\u6392\u5e8f<\/strong>\u201d\uff1a\u7d22\u5f15\u6392\u5e8f\u4f9d\u8d56 B + \u6811\u7684\u6709\u5e8f\u6027\uff0c\u9ad8\u6548\u4f4e\u8017\uff1b\u6587\u4ef6\u6392\u5e8f\u9700\u5185\u5b58 \/ \u78c1\u76d8\u6392\u5e8f\uff0c\u6027\u80fd\u5dee\u3002\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u901a\u8fc7 \u201c\u5efa\u5339\u914d\u7d22\u5f15 + \u7cbe\u7b80\u67e5\u8be2\u5b57\u6bb5 + \u63d0\u524d\u8fc7\u6ee4\u6570\u636e\u201d \u53ef\u663e\u8457\u63d0\u5347\u6392\u5e8f\u6027\u80fd\uff0c\u8fd9\u4e5f\u662f\u5904\u7406\u8ba2\u5355\u5217\u8868\u3001\u7528\u6237\u65e5\u5fd7\u7b49\u9700\u6392\u5e8f\u573a\u666f\u7684\u5e38\u89c4\u4f18\u5316\u624b\u6bb5\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h1>05. \u6162\u67e5\u8be2\u7684\u5b9a\u4f4d\u4e0e\u5206\u6790<\/h1>\n<p>MySQL \u7684\u6162\u67e5\u8be2\uff08\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u9608\u503c\u7684 SQL\uff09\u662f\u6027\u80fd\u4f18\u5316\u7684\u6838\u5fc3\u5207\u5165\u70b9\uff0c\u5b9a\u4f4d\u4e0e\u5206\u6790\u9700\u7ed3\u5408\u65e5\u5fd7\u5de5\u5177\u3001\u6267\u884c\u8ba1\u5212\u548c\u4e1a\u52a1\u573a\u666f\u3002<\/p>\n<h2>1. \u6162\u67e5\u8be2\u7684\u5b9a\u4f4d<\/h2>\n<p>\u6162\u67e5\u8be2\u7684\u5b9a\u4f4d\uff1a\u901a\u8fc7\u65e5\u5fd7\u6355\u83b7\u76ee\u6807 SQL\u3002<\/p>\n<p>\u5b9a\u4f4d\u6162\u67e5\u8be2\u7684\u6838\u5fc3\u662f<strong>\u5f00\u542f\u6162\u67e5\u8be2\u65e5\u5fd7<\/strong>\uff0c\u8bb0\u5f55\u6267\u884c\u65f6\u95f4\u8d85\u6807\u7684 SQL\uff0c\u4e3a\u540e\u7eed\u5206\u6790\u63d0\u4f9b\u4f9d\u636e\u3002<\/p>\n<p><strong>1. \u6162\u67e5\u8be2\u65e5\u5fd7\u914d\u7f6e\uff085.7 \u7248\u672c\uff09<\/strong><br \/>\n\u901a\u8fc7\u4fee\u6539my.cnf\uff08\u6216my.ini\uff09\u914d\u7f6e\uff0c\u5f00\u542f\u5e76\u5b9a\u5236\u65e5\u5fd7\u89c4\u5219\uff1a<\/p>\n<pre><code class=\"language-ini\">slow_query_log = 1  # \u5f00\u542f\u6162\u67e5\u8be2\u65e5\u5fd7\uff08\u9ed8\u8ba4\uff1a0\uff0c\u5173\u95ed\uff09\nslow_query_log_file = \/var\/log\/mysql\/slow.log  # \u65e5\u5fd7\u6587\u4ef6\u8def\u5f84\uff08\u9700MySQL\u6709\u5199\u5165\u6743\u9650\uff09\nlong_query_time = 1  # \u6162\u67e5\u8be2\u9608\u503c\uff08\u5355\u4f4d\u79d2\uff0c\u9ed8\u8ba410\u79d2\uff0c\u5efa\u8bae\u751f\u4ea7\u8bbe1-2\u79d2\uff09\nlog_queries_not_using_indexes = 1  # \u8bb0\u5f55\u672a\u4f7f\u7528\u7d22\u5f15\u7684\u67e5\u8be2\uff08\u5373\u4f7f\u672a\u8d85\u9608\u503c\uff0c\u53ef\u9009\uff09\nlog_output = FILE  # \u65e5\u5fd7\u8f93\u51fa\u65b9\u5f0f\uff08FILE\/table\uff0c5.7\u9ed8\u8ba4\uff1aFILE\uff09<\/code><\/pre>\n<p>\u914d\u7f6e\u540e\u91cd\u542f MySQL \u751f\u6548\uff0c\u4e5f\u53ef\u901a\u8fc7<code>SET GLOBAL<\/code>\u52a8\u6001\u5f00\u542f\uff08\u65e0\u9700\u91cd\u542f\uff0c\u9002\u5408\u4e34\u65f6\u6392\u67e5\uff09\uff1a<\/p>\n<pre><code class=\"language-sql\">SET GLOBAL slow_query_log = 1;\nSET GLOBAL long_query_time = 1;<\/code><\/pre>\n<div style=\"height:1em\"><\/div>\n<p><strong>2. \u65e5\u5fd7\u6536\u96c6\u5de5\u5177<\/strong><\/p>\n<ul>\n<li>\n<p><strong>mysqldumpslow\uff08\u81ea\u5e26\u5de5\u5177\uff09<\/strong>\uff1a\u7b80\u5355\u7edf\u8ba1\u6162\u67e5\u8be2\uff0c\u9002\u5408\u5feb\u901f\u5b9a\u4f4d\u9ad8\u9891\u95ee\u9898\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u67e5\u770b\u8bbf\u95ee\u6b21\u6570\u6700\u591a\u768410\u6761\u6162\u67e5\u8be2\nmysqldumpslow -s c -t 10 \/var\/log\/mysql\/slow.log\n# \u67e5\u770b\u603b\u6267\u884c\u65f6\u95f4\u6700\u957f\u768410\u6761\u6162\u67e5\u8be2\nmysqldumpslow -s t -t 10 \/var\/log\/mysql\/slow.log<\/code><\/pre>\n<p>\u4f18\u52bf\uff1a\u8f7b\u91cf\uff0c\u9002\u5408\u521d\u6b65\u7b5b\u9009\uff1b<br \/>\n\u4e0d\u8db3\uff1a\u65e0\u6cd5\u5206\u6790\u8d8b\u52bf\u6216\u590d\u6742\u7edf\u8ba1\u3002<\/p>\n<\/li>\n<li>\n<p><strong>pt-query-digest\uff08Percona \u5de5\u5177\u5305\uff09<\/strong>\uff1a\u751f\u4ea7\u73af\u5883\u5e38\u7528\uff0c\u53ef\u6309\u6267\u884c\u65f6\u95f4\u3001\u9891\u7387\u3001\u7528\u6237\u7b49\u7ef4\u5ea6\u5206\u6790\uff0c\u8f93\u51fa SQL \u6a21\u677f\u3001\u5e73\u5747\u8017\u65f6\u3001\u626b\u63cf\u884c\u6570\u7b49\u5173\u952e\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-bash\">pt-query-digest \/var\/log\/mysql\/slow.log &gt; slow_analysis.txt<\/code><\/pre>\n<p>\u4f18\u52bf\uff1a\u80fd\u8bc6\u522b\u91cd\u590d SQL\uff08\u5982\u53c2\u6570\u4e0d\u540c\u4f46\u7ed3\u6784\u76f8\u540c\u7684\u67e5\u8be2\uff09\uff0c\u5b9a\u4f4d \u201c\u9690\u5f62\u9ad8\u9891\u6162\u67e5\u8be2\u201d\uff08\u5355\u6761\u8017\u65f6 0.8 \u79d2\uff0c\u4f46\u6bcf\u79d2\u6267\u884c 100 \u6b21\uff0c\u603b\u8017\u65f6 80 \u79d2\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h2>2. \u6162\u67e5\u8be2\u7684\u5206\u6790<\/h2>\n<p>\u6162\u67e5\u8be2\u7684\u5206\u6790\uff1a\u901a\u8fc7\u6267\u884c\u8ba1\u5212\u5b9a\u4f4d\u6839\u56e0\u3002<\/p>\n<p>\u6355\u83b7\u6162\u67e5\u8be2\u540e\uff0c\u9700\u7528<code>EXPLAIN<\/code>\u5206\u6790\u5176\u6267\u884c\u903b\u8f91\uff0c\u91cd\u70b9\u5173\u6ce8\u662f\u5426\u5168\u8868\u626b\u63cf\u3001\u662f\u5426\u7528\u5bf9\u7d22\u5f15\u3001\u662f\u5426\u6709\u989d\u5916\u6392\u5e8f \/ \u4e34\u65f6\u8868\u3002<\/p>\n<p><strong>1. \u6838\u5fc3\u5206\u6790\u5de5\u5177\uff1a<code>EXPLAIN<\/code><\/strong><\/p>\n<p>\u5bf9\u6162\u67e5\u8be2\u6267\u884c<code>EXPLAIN<\/code>\uff0c\u67e5\u770b\u5173\u952e\u5b57\u6bb5\uff1a<\/p>\n<ul>\n<li><code>type<\/code>\uff1a\u8bbf\u95ee\u7c7b\u578b\uff08\u6027\u80fd\u4ece\u597d\u5230\u5dee\uff1a<code>const<\/code> &gt; <code>eq_ref<\/code> &gt; <code>ref<\/code> &gt; <code>range<\/code> &gt; <code>ALL<\/code>\uff09\u3002<code>ALL<\/code>\u8868\u793a\u5168\u8868\u626b\u63cf\uff08\u9700\u4f18\u5148\u4f18\u5316\uff09\u3002<\/li>\n<li><code>key<\/code>\uff1a\u5b9e\u9645\u4f7f\u7528\u7684\u7d22\u5f15\uff08<code>NULL<\/code>\u8868\u793a\u672a\u7528\u7d22\u5f15\uff09\u3002<\/li>\n<li><code>rows<\/code>\uff1a\u9884\u4f30\u626b\u63cf\u884c\u6570\uff08\u503c\u8d8a\u5927\uff0c\u6548\u7387\u8d8a\u4f4e\uff09\u3002<\/li>\n<li><code>Extra<\/code>\uff1a\u989d\u5916\u4fe1\u606f\uff08\u5173\u952e\u8b66\u544a\uff1a<code>Using filesort<\/code>\uff08\u9700\u6392\u5e8f\u4e14\u65e0\u7d22\u5f15\uff09\u3001<code>Using temporary<\/code>\uff08\u9700\u521b\u5efa\u4e34\u65f6\u8868\uff09\u3001<code>Using where<\/code>; <code>Using filesort<\/code>\uff08\u5168\u8868\u626b\u63cf\u540e\u6392\u5e8f\uff0c\u6027\u80fd\u6781\u5dee\uff09\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<p><strong>2. \u5e38\u89c1\u6162\u67e5\u8be2\u539f\u56e0\u53ca\u5206\u6790\uff08\u7ed3\u5408<code>EXPLAIN<\/code>\uff09<\/strong><\/p>\n<ul>\n<li><strong>\u5168\u8868\u626b\u63cf\uff08<code>type=ALL<\/code>\uff0c<code>key=NULL<\/code>\uff09<\/strong>\uff1a\n<ul>\n<li>\u539f\u56e0\uff1a\u67e5\u8be2\u6761\u4ef6\u65e0\u7d22\u5f15\uff08\u5982\uff1a<code>WHERE status=1<\/code>\u4f46<code>status<\/code>\u65e0\u7d22\u5f15\uff09\uff0c\u6216\u7d22\u5f15\u5931\u6548\uff08\u5982\uff1a<code>WHERE name LIKE &#039;%abc&#039;<\/code>\u5de6\u6a21\u7cca\u5339\u914d\u5bfc\u81f4\u7d22\u5f15\u5931\u6548\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong><code>JOIN<\/code> \u5173\u8054\u65e0\u7d22\u5f15\uff08<code>rows<\/code>\u6781\u5927\uff09<\/strong>\uff1a\n<ul>\n<li>\u539f\u56e0\uff1a\u88ab\u9a71\u52a8\u8868\u7684\u5173\u8054\u5b57\u6bb5\u65e0\u7d22\u5f15\uff08\u5982<code>t1 JOIN t2 ON t2.uid = t1.id<\/code>\uff0c<code>t2.uid<\/code>\u65e0\u7d22\u5f15\uff09\uff0c\u5bfc\u81f4\u88ab\u9a71\u52a8\u8868\u5168\u8868\u626b\u63cf\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u6392\u5e8f\u65e0\u7d22\u5f15\uff08<code>Extra=Using filesort<\/code>\uff09<\/strong>\uff1a\n<ul>\n<li>\u539f\u56e0\uff1a<code>ORDER BY<\/code>\u5b57\u6bb5\u65e0\u7d22\u5f15\uff0c\u6216\u6392\u5e8f\u987a\u5e8f\u4e0e\u7d22\u5f15\u4e0d\u4e00\u81f4\uff08\u5982\u7d22\u5f15\u662f<code>create_time ASC<\/code>\uff0c\u4f46\u67e5\u8be2\u7528<code>ORDER BY create_time DESC<\/code>\u4e14\u65e0\u964d\u5e8f\u7d22\u5f15\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5b50\u67e5\u8be2\u5d4c\u5957\uff08<code>type=DEPENDENT SUBQUERY<\/code>\uff09<\/strong>\uff1a\n<ul>\n<li>\u539f\u56e0\uff1a\u5173\u8054\u5b50\u67e5\u8be2\u5bfc\u81f4\u4e3b\u8868\u6bcf\u884c\u89e6\u53d1\u4e00\u6b21\u5b50\u67e5\u8be2\uff08\u5982<code>WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.uid = t1.id)<\/code>\uff09\uff0c\u65e0\u7d22\u5f15\u65f6\u6027\u80fd\u9aa4\u964d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h2>3. \u6162\u67e5\u8be2\u7684\u4f18\u5316\u65b9\u5411<\/h2>\n<p>\u5206\u6790\u51fa\u539f\u56e0\u540e\uff0c\u9488\u5bf9\u6027\u4f18\u5316\uff1a<\/p>\n<p><strong>1. \u52a0\u7d22\u5f15<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4e3a\u67e5\u8be2\u6761\u4ef6\u3001<code>JOIN<\/code> \u5173\u8054\u5b57\u6bb5\u3001\u6392\u5e8f\u5b57\u6bb5\u5efa\u7d22\u5f15\uff08\u5982\uff1a<code>WHERE<\/code>+<code>ORDER BY<\/code>\u5b57\u6bb5\u5efa\u8054\u5408\u7d22\u5f15\uff0c\u5982<code>idx_status_create_time (status, create_time)<\/code>\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>2. \u6539\u5199 SQL<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b50\u67e5\u8be2\u6539 <code>JOIN<\/code>\uff08\u5982\u5173\u8054\u5b50\u67e5\u8be2\u6539\u5199\u4e3a<code>INNER JOIN<\/code>\uff0c\u51cf\u5c11\u5d4c\u5957\u5faa\u73af\uff09\uff1b<\/li>\n<li>\u907f\u514d\u5de6\u6a21\u7cca\u5339\u914d\uff08<code>LIKE &#039;%abc&#039;<\/code>\u6539\u4e3a<code>LIKE &#039;abc%&#039;<\/code>\uff0c\u6216\u7528\u5168\u6587\u7d22\u5f15\uff09\uff1b<\/li>\n<li>\u5927\u5206\u9875\u6539\u8303\u56f4\u67e5\u8be2\uff08<code>LIMIT 100000,10<\/code>\u6539\u4e3a<code>WHERE id &gt; 100000 LIMIT 10<\/code>\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:0.7em\"><\/div>\n<p><strong>3. \u63a7\u5236\u6570\u636e\u91cf<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7528<code>WHERE<\/code>\u63d0\u524d\u8fc7\u6ee4\u65e0\u6548\u6570\u636e\uff08\u5982<code>WHERE create_time &gt; &#039;2024-01-01&#039;<\/code>\uff09\uff0c\u51cf\u5c11\u626b\u63cf\u548c\u6392\u5e8f\u884c\u6570\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h2>4. 8.0 \u7248\u672c\u7684\u4f18\u5316<\/h2>\n<ul>\n<li>\u6162\u67e5\u8be2\u65e5\u5fd7\u652f\u6301\u8868\u5b58\u50a8\uff1a\u9664\u6587\u4ef6\u5916\uff0c\u53ef\u5c06\u65e5\u5fd7\u5199\u5165<code>mysql.slow_log<\/code>\u8868\uff08<code>log_output = TABLE<\/code>\uff09\uff0c\u652f\u6301 SQL \u67e5\u8be2\u5206\u6790\uff08\u5982\u6309\u65f6\u95f4\u8303\u56f4\u7edf\u8ba1\uff09\u3002<\/li>\n<li>\u65e5\u5fd7\u5185\u5bb9\u66f4\u4e30\u5bcc\uff1a\u65b0\u589e<code>query_id<\/code>\uff08\u552f\u4e00\u6807\u8bc6\uff09\u3001<code>rows_affected<\/code>\u7b49\u5b57\u6bb5\uff0c\u65b9\u4fbf\u5173\u8054\u6027\u80fd\u6a21\u5f0f\uff08<code>Performance Schema<\/code>\uff09\u6570\u636e\u3002<\/li>\n<li>\u52a8\u6001\u8c03\u6574\u66f4\u7075\u6d3b\uff1a<code>long_query_time<\/code>\u652f\u6301\u5c0f\u6570\uff08\u5982 0.5 \u79d2\uff09\uff0c\u4e14\u4fee\u6539\u540e\u7acb\u5373\u751f\u6548\uff08\u65e0\u9700\u91cd\u542f\u8fde\u63a5\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h2>5. \u603b\u7ed3<\/h2>\n<p>\u6162\u67e5\u8be2\u5b9a\u4f4d\u4e0e\u5206\u6790\u7684\u6838\u5fc3\u6d41\u7a0b\u662f\uff1a<strong>\u5f00\u542f\u65e5\u5fd7\u6355\u83b7 \u2192 \u5de5\u5177\u7edf\u8ba1\u7b5b\u9009 \u2192 <code>EXPLAIN<\/code>\u5206\u6790\u6267\u884c\u8ba1\u5212 \u2192 \u9488\u5bf9\u6027\u4f18\u5316\uff08\u7d22\u5f15 \/ \u6539\u5199 SQL\uff09<\/strong>\u3002<\/p>\n<p>\u5b9e\u8df5\u4e2d\uff0c\u9700\u7ed3\u5408\u4e1a\u52a1\u573a\u666f\uff08\u5982\u8ba2\u5355\u8868\u6162\u67e5\u8be2\u591a\u4e0e<code>user_id<\/code>\u3001<code>create_time<\/code>\u7d22\u5f15\u76f8\u5173\uff09\uff0c\u4f18\u5148\u89e3\u51b3 \u201c\u9ad8\u9891 + \u9ad8\u8017\u65f6\u201d \u7684\u6162\u67e5\u8be2\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h1>06. \u590d\u6742SQL\u7684\u62c6\u5206\u4e0e\u6539\u5199<\/h1>\n<p><strong>\u5927\u8868\u5206\u9875\u4f18\u5316\uff1a\u5ef6\u8fdf\u5173\u8054\u3001\u4e66\u7b7e\u5206\u9875\u3002<\/strong><\/p>\n<p>MySQL \u5927\u8868\u5206\u9875\u662f\u4e1a\u52a1\u4e2d\u9ad8\u9891\u75db\u70b9\uff08\u5982\u8ba2\u5355\u5217\u8868\u3001\u65e5\u5fd7\u67e5\u8be2\uff09\uff0c\u666e\u901a<code>LIMIT offset, size<\/code>\u5728<code>offset<\/code>\u8f83\u5927\u65f6\uff08\u5982<code>LIMIT 100000,10<\/code>\uff09\u6027\u80fd\u6781\u5dee\uff1a\u56e0 MySQL \u9700\u626b\u63cf\u524d 10 \u4e07\u884c\u518d\u4e22\u5f03\uff0c\u4ec5\u53d6\u6700\u540e 10 \u884c\uff0cIO \u5f00\u9500\u5267\u589e\u3002<\/p>\n<p>\u590d\u6742 SQL \u62c6\u5206\u4e0e\u6539\u5199\u7684\u6838\u5fc3\u601d\u8def\u662f<strong>\u51cf\u5c11\u65e0\u6548\u626b\u63cf\u884c\u6570<\/strong>\uff0c\u5e38\u7528\u4f18\u5316\u624b\u6bb5\u5305\u62ec<strong>\u5ef6\u8fdf\u5173\u8054<\/strong>\u3001<strong>\u4e66\u7b7e\u5206\u9875<\/strong>\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>1. \u666e\u901a\u5206\u9875\u7684\u6027\u80fd\u74f6\u9888<\/h2>\n<p>\u666e\u901a\u5206\u9875 SQL \u793a\u4f8b\uff08\u8ba2\u5355\u8868<code>order<\/code>\uff0c\u6309<code>create_time<\/code>\u9006\u5e8f\uff09\uff1a<\/p>\n<pre><code class=\"language-sql\">SELECT * FROM `order` ORDER BY create_time DESC LIMIT 100000, 10;<\/code><\/pre>\n<p>\u95ee\u9898\uff1a\u5f53<code>offset=100000<\/code>\u65f6\uff0cMySQL \u9700\u5148\u904d\u5386\u7d22\u5f15\u627e\u5230\u7b2c 10 \u4e07\u884c\u7684\u4f4d\u7f6e\uff08\u5373\u4f7f\u6709<code>create_time<\/code>\u7d22\u5f15\uff09\uff0c\u518d\u56de\u8868\u8bfb\u53d6 10 \u884c\u6570\u636e\uff0c\u524d 10 \u4e07\u884c\u7684\u626b\u63cf\u5c5e\u4e8e\u65e0\u6548\u5f00\u9500\uff0c\u6570\u636e\u91cf\u8d8a\u5927\u8d8a\u6162\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>2. \u5173\u952e\u4f18\u5316\u65b9\u6cd5<\/h2>\n<h3>1. \u5ef6\u8fdf\u5173\u8054\uff08\u5ef6\u8fdf\u56de\u8868\uff09<\/h3>\n<p><strong>\u6982\u5ff5\uff1a<\/strong>\u5148\u901a\u8fc7<strong>\u8986\u76d6\u7d22\u5f15<\/strong>\u83b7\u53d6\u76ee\u6807\u6570\u636e\u7684\u4e3b\u952e\uff08\u6216\u552f\u4e00\u952e\uff09\uff0c\u518d\u5173\u8054\u539f\u8868\u83b7\u53d6\u5b8c\u6574\u5b57\u6bb5\uff0c\u907f\u514d \u201c\u65e9\u671f\u56de\u8868\u201d \u5e26\u6765\u7684\u5927\u91cf IO\u3002<\/p>\n<p><strong>\u539f\u7406\uff1a<\/strong>\u5229\u7528\u7d22\u5f15\u7684 \u201c\u8986\u76d6\u67e5\u8be2\u201d \u7279\u6027\uff08\u53ea\u67e5\u4e3b\u952e\uff0c\u65e0\u9700\u56de\u8868\uff09\u5feb\u901f\u5b9a\u4f4d\u76ee\u6807\u884c\uff0c\u518d\u901a\u8fc7\u4e3b\u952e\u5173\u8054\u539f\u8868\u53d6\u6570\uff0c\u51cf\u5c11\u626b\u63cf\u548c\u56de\u8868\u7684\u6570\u636e\u91cf\u3002<\/p>\n<p><strong>\u4f18\u5316\u540e SQL \u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-sql\">-- \u5b50\u67e5\u8be2\u7528\u7d22\u5f15\u83b7\u53d6\u4e3b\u952e\uff08\u8986\u76d6\u7d22\u5f15\uff0c\u65e0\u9700\u56de\u8868\uff09\nSELECT o.* \nFROM `order` o \nJOIN (SELECT id FROM `order` ORDER BY create_time DESC LIMIT 100000, 10) tmp \nON o.id = tmp.id;<\/code><\/pre>\n<ul>\n<li>\u4f18\u52bf\uff1a\u5b50\u67e5\u8be2\u4ec5\u626b\u63cf\u7d22\u5f15\u83b7\u53d6id\uff08\u8f7b\u91cf\uff09\uff0c\u5173\u8054\u539f\u8868\u65f6\u4ec5\u53d6 10 \u884c\u5b8c\u6574\u6570\u636e\uff0c\u6027\u80fd\u6bd4\u666e\u901a\u5206\u9875\u63d0\u5347 5-10 \u500d\uff0810 \u4e07 + offset \u573a\u666f\uff09\u3002<\/li>\n<li>\u9002\u7528\u573a\u666f\uff1a\u9700\u5c55\u793a\u5b8c\u6574\u5b57\u6bb5\u3001\u652f\u6301\u4efb\u610f\u8df3\u9875\uff08\u5982\u7b2c 100 \u9875\u3001\u7b2c 200 \u9875\uff09\u7684\u5217\u8868\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h3>2. \u4e66\u7b7e\u5206\u9875\uff08\u6e38\u6807\u5206\u9875 \/ \u952e\u96c6\u5206\u9875\uff09<\/h3>\n<p><strong>\u6982\u5ff5\uff1a<\/strong>\u7528 \u201c\u4e0a\u4e00\u9875\u6700\u540e\u4e00\u6761\u8bb0\u5f55\u7684\u552f\u4e00\u6807\u8bc6\u201d\uff08\u4e66\u7b7e\uff0c\u5982<code>id<\/code>\u6216<code>create_time+id<\/code>\uff09\u4f5c\u4e3a\u6761\u4ef6\uff0c\u66ff\u4ee3<code>offset<\/code>\u76f4\u63a5\u5b9a\u4f4d\u4e0b\u4e00\u9875\u8d77\u70b9\uff0c\u5b8c\u5168\u907f\u514d\u65e0\u6548\u626b\u63cf\u3002<\/p>\n<p><strong>\u539f\u7406\uff1a<\/strong>\u5229\u7528\u4e3b\u952e \/ \u552f\u4e00\u7d22\u5f15\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7<code>WHERE<\/code>\u6761\u4ef6\u8df3\u8fc7\u524d\u9762\u6570\u636e\uff0c\u76f4\u63a5\u4ece\u4e66\u7b7e\u4f4d\u7f6e\u53d6\u6570\u3002<\/p>\n<p><strong>\u4f18\u5316\u540e SQL \u793a\u4f8b\uff1a<\/strong>\u5047\u8bbe\u4e0a\u4e00\u9875\u6700\u540e\u4e00\u6761\u8bb0\u5f55\u7684<code>create_time=&#039;2024-01-01 10:00:00&#039;<\/code>\u3001<code>id=100000<\/code>\uff0c\u4e0b\u4e00\u9875\u67e5\u8be2\uff1a<\/p>\n<pre><code class=\"language-sql\">SELECT * \nFROM `order` \nWHERE create_time &lt; &#039;2024-01-01 10:00:00&#039; \n   OR (create_time = &#039;2024-01-01 10:00:00&#039; AND id &lt; 100000) \nORDER BY create_time DESC, id DESC \nLIMIT 10;<\/code><\/pre>\n<ul>\n<li>\u5173\u952e\uff1a\u7528<code>create_time+id<\/code>\u4f5c\u4e3a\u4e66\u7b7e\uff08<code>create_time<\/code>\u53ef\u80fd\u91cd\u590d\uff0c\u9700\u52a0<code>id<\/code>\u4fdd\u8bc1\u552f\u4e00\u6027\uff09\uff0c\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u5b9a\u4f4d\uff0c\u65e0<code>offset<\/code>\u5f00\u9500\u3002<\/li>\n<li>\u4f18\u52bf\uff1a\u6027\u80fd\u6781\u81f4\uff08\u65e0\u8bba\u7ffb\u591a\u5c11\u9875\uff0c\u626b\u63cf\u884c\u6570\u56fa\u5b9a\u4e3a<code>LIMIT size<\/code>\uff09\uff1b<\/li>\n<li>\u5c40\u9650\uff1a\u4e0d\u652f\u6301 \u201c\u8df3\u9875\u201d\uff08\u5982\u76f4\u63a5\u5230\u7b2c 100 \u9875\uff09\uff0c\u4ec5\u9002\u5408 \u201c\u4e0a\u4e00\u9875 \/ \u4e0b\u4e00\u9875\u201d\u201c\u52a0\u8f7d\u66f4\u591a\u201d \u573a\u666f\uff08APP \/ \u79fb\u52a8\u7aef\u5e38\u7528\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h3>3. \u8986\u76d6\u7d22\u5f15\u5206\u9875<\/h3>\n<p>\u8986\u76d6\u7d22\u5f15\u5206\u9875\uff1a\u82e5\u67e5\u8be2\u5b57\u6bb5\uff08\u5982<code>id<\/code>, <code>order_no<\/code>, <code>create_time<\/code>\uff09\u5168\u90e8\u5305\u542b\u5728\u7d22\u5f15\u4e2d\uff0c\u76f4\u63a5\u7528\u7d22\u5f15\u67e5\u8be2\uff0c\u65e0\u9700\u56de\u8868\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u7d22\u5f15`idx_create_time_order_no (create_time DESC, order_no, id)`\u8986\u76d6\u6240\u6709\u67e5\u8be2\u5b57\u6bb5\nSELECT id, order_no, create_time \nFROM `order` \nORDER BY create_time DESC \nLIMIT 100000, 10;<\/code><\/pre>\n<div style=\"height:1em\"><\/div>\n<h3>4. \u9650\u5236\u6700\u5927 Offset<\/h3>\n<p>\u4e1a\u52a1\u4e0a\u7981\u6b62<code>offset<\/code>\u8d85\u8fc7\u9608\u503c\uff08\u5982 10000\uff09\uff0c\u5f15\u5bfc\u7528\u6237\u901a\u8fc7\u7b5b\u9009\u6761\u4ef6\uff08\u5982 \u201c\u6309\u65f6\u95f4\u8303\u56f4\u7b5b\u9009\u201d\uff09\u7f29\u5c0f\u6570\u636e\u8303\u56f4\uff0c\u907f\u514d\u5927 <code>offset<\/code> \u67e5\u8be2\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>5. \u5206\u8868\u5206\u9875<\/h3>\n<p>\u8d85\u5927\u89c4\u6a21\u8868\uff08\u4ebf\u7ea7\uff09\u9700\u6c34\u5e73\u5206\u8868\uff08\u5982\u6309<code>create_time<\/code>\u5206\u8868\uff09\uff0c\u5206\u9875\u65f6\u5148\u5b9a\u4f4d\u5206\u8868\uff0c\u518d\u5728\u5206\u8868\u5185\u5206\u9875\uff0c\u907f\u514d\u8de8\u8868\u626b\u63cf\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>3. \u603b\u7ed3<\/h2>\n<p>\u5927\u8868\u5206\u9875\u4f18\u5316\u7684\u6838\u5fc3\u662f <strong>\u201c\u7528\u7d22\u5f15\u5b9a\u4f4d\u66ff\u4ee3\u65e0\u6548\u626b\u63cf\u201d<\/strong>\uff1a<\/p>\n<ul>\n<li>\u9700\u652f\u6301\u8df3\u9875\u9009<strong>\u5ef6\u8fdf\u5173\u8054<\/strong>\uff0c\u901a\u8fc7\u8986\u76d6\u7d22\u5f15\u51cf\u5c11\u56de\u8868\uff1b<\/li>\n<li>\u79fb\u52a8\u7aef \/ \u52a0\u8f7d\u66f4\u591a\u9009<strong>\u4e66\u7b7e\u5206\u9875<\/strong>\uff0c\u7528\u552f\u4e00\u952e\u76f4\u63a5\u5b9a\u4f4d\uff1b<\/li>\n<li>\u6781\u81f4\u6027\u80fd\u9009<strong>\u8986\u76d6\u7d22\u5f15\u5206\u9875<\/strong>\uff0c\u907f\u514d\u56de\u8868\u5f00\u9500\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h1>07. <code>JOIN<\/code>\u4f18\u5316<\/h1>\n<p>\u5c0f\u8868\u9a71\u52a8\u5927\u8868\u3001 \u907f\u514d <code>cross join<\/code>\u3002<\/p>\n<h2>1. <code>JOIN<\/code> \u4f18\u5316\u7684\u6838\u5fc3\u903b\u8f91<\/h2>\n<p>MySQL \u4e2d <code>JOIN<\/code> \u7684\u5e95\u5c42\u5b9e\u73b0\u4ee5<strong><code>Nested Loop Join<\/code>\uff08\u5d4c\u5957\u5faa\u73af\u8fde\u63a5\uff09\u4e3a\u4e3b\uff1a\u5916\u5c42\u5faa\u73af\u904d\u5386 \u201c\u9a71\u52a8\u8868\u201d \u7684\u6570\u636e\uff0c\u5185\u5c42\u5faa\u73af\u7528\u9a71\u52a8\u8868\u7684\u5b57\u6bb5\u53bb \u201c\u88ab\u9a71\u52a8\u8868\u201d \u4e2d\u5339\u914d\u6570\u636e\u3002<\/strong>\u3002\u5c31\u50cfJava\u91cc\u7684\u53cc\u5c42<code>for<\/code>\u5faa\u73af\u3002<\/p>\n<p>\u4f18\u5316\u7684\u6838\u5fc3\u662f<strong>\u51cf\u5c11\u5916\u5c42\u5faa\u73af\u6b21\u6570 + \u964d\u4f4e\u5185\u5c42\u5339\u914d\u6210\u672c<\/strong>\uff0c\u8fd9\u4e5f\u662f \u201c\u5c0f\u8868\u9a71\u52a8\u5927\u8868\u201d \u7684\u539f\u7406\u57fa\u7840\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>2. \u4f18\u53161\uff1a\u5c0f\u8868\u9a71\u52a8\u5927\u8868<\/h2>\n<p><strong>1. \u539f\u7406<\/strong><\/p>\n<p>\u201c\u5c0f\u8868\u201d \u6307\u53c2\u4e0e <code>JOIN<\/code> \u7684\u6570\u636e\u96c6\u66f4\u5c0f\u7684\u8868\uff08\u800c\u975e\u7269\u7406\u8868\u5927\u5c0f\uff09\u3002<br \/>\n\u7528\u5c0f\u8868\u505a\u9a71\u52a8\u8868\uff08\u5916\u5c42\u5faa\u73af\uff09\uff0c\u5927\u8868\u505a\u88ab\u9a71\u52a8\u8868\uff08\u5185\u5c42\u5faa\u73af\uff09\uff0c\u53ef\u663e\u8457\u51cf\u5c11\u5916\u5c42\u5faa\u73af\u6b21\u6570\uff0c\u4ece\u800c\u964d\u4f4e\u6574\u4f53 IO \u548c\u5339\u914d\u5f00\u9500\u3002<\/p>\n<ul>\n<li>\u4e3e\u4f8b\uff1a\u5c0f\u8868\u6709 100 \u884c\uff0c\u5927\u8868\u6709 100 \u4e07\u884c\n<ul>\n<li>\u5c0f\u8868\u9a71\u52a8\u5927\u8868\uff1a\u5916\u5c42\u5faa\u73af 100 \u6b21\uff0c\u5185\u5c42\u6bcf\u6b21\u5339\u914d\u5927\u8868 \u2192 \u603b\u5339\u914d 100 \u6b21\uff1b<\/li>\n<li>\u5927\u8868\u9a71\u52a8\u5c0f\u8868\uff1a\u5916\u5c42\u5faa\u73af 100 \u4e07\u6b21\uff0c\u5185\u5c42\u6bcf\u6b21\u5339\u914d\u5c0f\u8868 \u2192 \u603b\u5339\u914d 100 \u4e07\u6b21\uff1b<\/li>\n<li>\u6027\u80fd\u5dee\u5f02\u4e00\u76ee\u4e86\u7136\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<p><strong>2. \u5b9e\u8df5\u5224\u65ad\u4e0e\u793a\u4f8b<\/strong><\/p>\n<ul>\n<li><strong>\u5982\u4f55\u5224\u65ad \u201c\u5c0f\u8868\u201d\uff1a<\/strong>\n<ul>\n<li>\u901a\u8fc7<code>EXPLAIN<\/code>\u770b<code>rows<\/code>\u5b57\u6bb5\uff08\u9884\u4f30\u626b\u63cf\u884c\u6570\uff09\uff0c\u884c\u6570\u5c11\u7684\u5c31\u662f \u201c\u5c0f\u8868\u201d\uff1b<\/li>\n<li>\u6216\u4e1a\u52a1\u903b\u8f91\u4e2d\u660e\u786e\u6570\u636e\u91cf\u66f4\u5c0f\u7684\u8868\uff08\u5982\u5b57\u5178\u8868\u3001\u914d\u7f6e\u8868\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>SQL \u793a\u4f8b\uff08\u7528\u6237\u8868<code>user<\/code>\u662f\u5c0f\u8868\uff0c\u8ba2\u5355\u8868<code>order<\/code>\u662f\u5927\u8868\uff09\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">-- \u63a8\u8350\uff1a\u5c0f\u8868user\u9a71\u52a8\u5927\u8868order\uff08INNER JOIN\u4e2dMySQL\u4f1a\u81ea\u52a8\u4f18\u5316\uff0c\u4f46LEFT JOIN\u9700\u663e\u5f0f\u63a7\u5236\uff09\nSELECT u.username, o.order_no \nFROM user u \nINNER JOIN `order` o ON u.id = o.user_id;\n\n-- LEFT JOIN\u9700\u6ce8\u610f\uff1a\u5de6\u8868\u662f\u9a71\u52a8\u8868\uff0c\u56e0\u6b64\u5de6\u8868\u5fc5\u987b\u662f\u5c0f\u8868\nSELECT u.username, o.order_no \nFROM user u  -- \u5c0f\u8868\u653e\u5de6\u8868\uff08\u9a71\u52a8\u8868\uff09\nLEFT JOIN `order` o ON u.id = o.user_id;<\/code><\/pre>\n<div style=\"height:1em\"><\/div>\n<p><strong>3. \u6ce8\u610f\u70b9<\/strong><\/p>\n<ul>\n<li><code>INNER JOIN<\/code>\uff1aMySQL \u4f18\u5316\u5668\u4f1a\u81ea\u52a8\u9009\u62e9\u5c0f\u8868\u4f5c\u4e3a\u9a71\u52a8\u8868\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u6574\uff1b<\/li>\n<li><code>LEFT JOIN<\/code>\uff1a\u9a71\u52a8\u8868\u56fa\u5b9a\u4e3a\u5de6\u8868\uff0c\u56e0\u6b64\u5fc5\u987b\u5c06\u5c0f\u8868\u653e\u5728\u5de6\u8868\u4f4d\u7f6e\uff08\u5426\u5219\u4f1a\u7528\u5927\u8868\u9a71\u52a8\u5c0f\u8868\uff0c\u6027\u80fd\u66b4\u8dcc\uff09\uff1b<\/li>\n<li><code>RIGHT JOIN<\/code>\uff1a\u9a71\u52a8\u8868\u56fa\u5b9a\u4e3a\u53f3\u8868\uff0c\u9700\u5c06\u5c0f\u8868\u653e\u5728\u53f3\u8868\u4f4d\u7f6e\uff08\u5b9e\u9645\u5f00\u53d1\u4e2d\u5efa\u8bae\u7528<code>LEFT JOIN<\/code>\u66ff\u4ee3\uff0c\u66f4\u7b26\u5408\u9605\u8bfb\u4e60\u60ef\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>3. \u4f18\u53162\uff1a\u907f\u514d <code>CROSS JOIN<\/code>\uff08\u7b1b\u5361\u5c14\u79ef\uff09<\/h2>\n<p><strong>1. <code>CROSS JOIN<\/code> \u7684\u5371\u5bb3<\/strong><\/p>\n<p><code>CROSS JOIN<\/code>\u4f1a\u8fd4\u56de\u4e24\u5f20\u8868\u7684<strong>\u7b1b\u5361\u5c14\u79ef<\/strong>\uff08\u884c\u6570 = \u8868 A \u884c\u6570 \u00d7 \u8868 B \u884c\u6570\uff09\uff0c\u82e5\u8868 A \u6709 1 \u4e07\u884c\u3001\u8868 B \u6709 10 \u4e07\u884c\uff0c\u7ed3\u679c\u4f1a\u6709 10 \u4ebf\u884c\uff0c\u76f4\u63a5\u5bfc\u81f4\u6570\u636e\u5e93 CPU\/IO \u6253\u6ee1\u3001\u67e5\u8be2\u8d85\u65f6\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<p><strong>2. \u5e38\u89c1\u89e6\u53d1\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u663e\u5f0f\u4f7f\u7528<code>CROSS JOIN<\/code>\u5173\u952e\u5b57\u4e14\u65e0<code>ON<\/code>\u6761\u4ef6\uff1b<\/li>\n<li><code>JOIN<\/code>\u65f6\u9057\u6f0f<code>ON<\/code>\u5173\u8054\u6761\u4ef6\uff08\u5982<code>SELECT * FROM A JOIN B<\/code>\uff09\uff1b<\/li>\n<li><code>WHERE<\/code>\u6761\u4ef6\u65e0\u6cd5\u8fc7\u6ee4\u7b1b\u5361\u5c14\u79ef\uff08\u5982\u5173\u8054\u5b57\u6bb5\u503c\u5168\u4e3a <code>NULL<\/code>\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<p><strong>3. \u5982\u4f55\u907f\u514d<\/strong><\/p>\n<ul>\n<li>\u4efb\u4f55<code>JOIN<\/code>\u5fc5\u987b\u52a0<code>ON<\/code>\u5173\u8054\u6761\u4ef6\uff08\u5373\u4f7f\u662f<code>INNER JOIN<\/code>\uff09\uff1b<\/li>\n<li>\u7981\u6b62\u663e\u5f0f\u4f7f\u7528\u65e0\u5173\u8054\u6761\u4ef6\u7684<code>CROSS JOIN<\/code>\uff1b<\/li>\n<li>\u82e5\u9700\u5173\u8054\u4f46\u65e0\u76f4\u63a5\u5b57\u6bb5\uff0c\u901a\u8fc7\u4e1a\u52a1\u903b\u8f91\u8865\u5145\u5173\u8054\u6761\u4ef6\uff08\u5982\u65f6\u95f4\u8303\u56f4\u3001\u72b6\u6001\u8fc7\u6ee4\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>4. \u4f18\u5316\u8865\u5145<\/h2>\n<p><strong>1. \u88ab\u9a71\u52a8\u8868\u7684\u5173\u8054\u5b57\u6bb5\u5fc5\u987b\u52a0\u7d22\u5f15<\/strong><br \/>\n\u88ab\u9a71\u52a8\u8868\u7684<code>ON<\/code>\u5b57\u6bb5\uff08\u5982<code>order.user_id<\/code>\uff09\u9700\u5efa\u7d22\u5f15\uff0c\u5426\u5219\u5185\u5c42\u5faa\u73af\u4f1a\u5168\u8868\u626b\u63cf\uff0c\u5373\u4f7f\u5c0f\u8868\u9a71\u52a8\u5927\u8868\u4e5f\u4f1a\u5f88\u6162\u3002<\/p>\n<p><strong>2. \u907f\u514d<code>SELECT *<\/code><\/strong><br \/>\n\u53ea\u67e5\u8be2\u9700\u8981\u7684\u5b57\u6bb5\uff0c\u51cf\u5c11\u6570\u636e\u4f20\u8f93\u91cf\uff08\u5c24\u5176\u5927\u8868\u6709\u5927\u5b57\u6bb5\u5982<code>TEXT<\/code>\u65f6\uff09\u3002<\/p>\n<p><strong>3. \u63d0\u524d\u8fc7\u6ee4\u6570\u636e<\/strong><br \/>\n\u7528<code>WHERE<\/code>\u5148\u8fc7\u6ee4\u9a71\u52a8\u8868\u548c\u88ab\u9a71\u52a8\u8868\u7684\u65e0\u6548\u6570\u636e\uff08\u5982<code>WHERE o.status=1<\/code>\uff09\uff0c\u51cf\u5c11\u53c2\u4e0e <code>JOIN<\/code> \u7684\u884c\u6570\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>5. \u603b\u7ed3<\/h2>\n<p>MySQL <code>JOIN<\/code> \u4f18\u5316\u7684\u6838\u5fc3\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u5c0f\u8868\u9a71\u52a8\u5927\u8868\uff1a<\/strong>\u5229\u7528\u5d4c\u5957\u5faa\u73af\u7684\u7279\u6027\u51cf\u5c11\u5916\u5c42\u5faa\u73af\u6b21\u6570\uff0c<code>LEFT JOIN<\/code>\u9700\u624b\u52a8\u63a7\u5236\u9a71\u52a8\u8868\uff1b<\/li>\n<li><strong>\u675c\u7edd\u7b1b\u5361\u5c14\u79ef\uff1a<\/strong><code>JOIN<\/code>\u5fc5\u987b\u52a0<code>ON<\/code>\u6761\u4ef6\uff0c\u907f\u514d<code>CROSS JOIN<\/code>\uff1b<\/li>\n<li><strong>\u7d22\u5f15\u515c\u5e95\uff1a<\/strong>\u88ab\u9a71\u52a8\u8868\u7684\u5173\u8054\u5b57\u6bb5\u52a0\u7d22\u5f15\uff0c\u662f <code>JOIN<\/code> \u6027\u80fd\u7684\u57fa\u7840\u4fdd\u969c\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<p><a id=\"count\u7684\u6027\u80fd\u5dee\u5f02\" class=\"offset-anchor\"><\/a><\/p>\n<h1>08. count\u7684\u6027\u80fd\u5dee\u5f02<\/h1>\n<p>count(1)\u3001count(*)\u3001count(\u5b57\u6bb5)<\/p>\n<h2>1. <code>count<\/code> \u7684\u672c\u8d28<\/h2>\n<p><code>count<\/code> \u7684\u672c\u8d28\uff1a<strong>\u7edf\u8ba1 \u201c\u975e <code>NULL<\/code> \u884c\u6570\u201d\u3002<\/strong><\/p>\n<p><code>count()<\/code>\u662f\u805a\u5408\u51fd\u6570\uff0c\u6838\u5fc3\u4f5c\u7528\u662f<strong>\u7edf\u8ba1\u67e5\u8be2\u7ed3\u679c\u96c6\u4e2d\u7b26\u5408\u6761\u4ef6\u7684 \u201c\u975e <code>NULL<\/code> \u884c\u6570\u201d<\/strong>\uff0c\u4e0d\u540c\u53c2\u6570\u7684\u5dee\u5f02\u5728\u4e8e \u201c\u7edf\u8ba1\u8303\u56f4\u201d \u548c \u201c\u662f\u5426\u9700\u8981\u5224\u65ad <code>NULL<\/code>\u201d\uff0c\u8fd9\u76f4\u63a5\u51b3\u5b9a\u6027\u80fd\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>2. \u4e09\u79cd <code>count<\/code> \u7684\u533a\u522b\u4e0e\u6027\u80fd\u5bf9\u6bd4<\/h2>\n<h3>1. <code>count(*)<\/code><\/h3>\n<p><strong>\u7edf\u8ba1\u6240\u6709\u884c\u6570\uff08\u542b <code>NULL<\/code>\uff09\u3002<\/strong><\/p>\n<p><strong>\u903b\u8f91\uff1a<\/strong>\u4e13\u95e8\u7528\u4e8e\u7edf\u8ba1\u8868 \/ \u7ed3\u679c\u96c6\u7684\u603b\u884c\u6570\u3002<br \/>\nMySQL \u5bf9\u5176\u6709\u7279\u6b8a\u4f18\u5316\uff1a\u4f1a\u4f18\u5148\u9009\u62e9\u6700\u5c0f\u7684\u975e\u805a\u96c6\u7d22\u5f15\uff08\u8986\u76d6\u7d22\u5f15\uff09\u626b\u63cf\uff0c\u82e5\u6ca1\u6709\u5219\u7528\u805a\u96c6\u7d22\u5f15\uff0c\u907f\u514d\u5168\u8868\u626b\u63cf\u3002<\/p>\n<p><strong>\u7279\u70b9\uff1a<\/strong>\u5305\u542b <code>NULL<\/code> \u503c\uff08\u56e0\u4e3a\u4e0d\u5224\u65ad\u5b57\u6bb5\uff0c\u53ea\u7edf\u8ba1\u884c\u6570\uff09\uff0c\u6027\u80fd\u6700\u4f18\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>2. <code>count(1)<\/code><\/h3>\n<p><strong>\u7edf\u8ba1\u6240\u6709\u884c\u6570\uff08\u5360\u4f4d\u7b26\uff09\u3002<\/strong><\/p>\n<p><strong>\u903b\u8f91\uff1a<\/strong>\u7528\u5e38\u91cf1\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u7edf\u8ba1\u6240\u6709\u884c\u6570\uff08\u65e0\u8bba\u5b57\u6bb5\u662f\u5426\u4e3a <code>NULL<\/code>\uff09\u3002<br \/>\nMySQL \u4f18\u5316\u5668\u4f1a\u5c06\u5176\u4e0e<code>count(*)<\/code>\u89c6\u4e3a\u7b49\u4ef7\uff0c\u5e95\u5c42\u6267\u884c\u8ba1\u5212\u5b8c\u5168\u76f8\u540c\u3002<\/p>\n<p><strong>\u7279\u70b9\uff1a<\/strong>\u6027\u80fd\u4e0e<code>count(*)<\/code>\u51e0\u4e4e\u65e0\u5dee\u5f02\uff0c\u5c5e\u4e8e \u201c\u8bed\u6cd5\u7cd6\u201d\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>3. <code>count(\u5b57\u6bb5)<\/code><\/h3>\n<p><strong>\u7edf\u8ba1\u5b57\u6bb5\u975e <code>NULL<\/code> \u7684\u884c\u6570\u3002<\/strong><\/p>\n<p><strong>\u903b\u8f91\uff1a<\/strong>\u9700\u9010\u884c\u68c0\u67e5\u8be5\u5b57\u6bb5\u662f\u5426\u4e3a <code>NULL<\/code>\uff0c\u4ec5\u7edf\u8ba1\u975e <code>NULL<\/code> \u7684\u884c\u6570\u3002\u6027\u80fd\u5206\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li><strong>\u5b57\u6bb5\u662f\u7d22\u5f15\u5b57\u6bb5\uff1a<\/strong>\u7528\u7d22\u5f15\u626b\u63cf\uff08\u6bd4\u5168\u8868\u5feb\uff09\uff0c\u4f46\u9700\u5224\u65ad <code>NULL<\/code>\uff1b<\/li>\n<li><strong>\u5b57\u6bb5\u662f\u975e\u7d22\u5f15\u5b57\u6bb5\uff1a<\/strong>\u5168\u8868\u626b\u63cf + \u5224\u65ad <code>NULL<\/code>\uff0c\u6027\u80fd\u6700\u5dee\u3002<\/li>\n<\/ul>\n<p><strong>\u7279\u70b9\uff1a<\/strong>\u6027\u80fd\u4f4e\u4e8e<code>count(*)<\/code>\/<code>count(1)<\/code>\uff0c\u4e14\u7ed3\u679c\u53ef\u80fd\u4e0d\u7b49\u4e8e\u603b\u884c\u6570\uff08\u56e0\u4e3a\u5b57\u6bb5\u53ef\u80fd\u4e3a <code>NULL<\/code>\uff09\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h2>3. \u6027\u80fd\u6392\u5e8f<\/h2>\n<p>\u4ece\u5feb\u5230\u6162\uff1a<code>count(*)<\/code> \u2248 <code>count(1)<\/code> &gt; <code>ount(\u7d22\u5f15\u5b57\u6bb5)<\/code> &gt; <code>count(\u975e\u7d22\u5f15\u5b57\u6bb5)<\/code><\/p>\n<p>\u5173\u952e\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><code>count(*)<\/code>\/<code>count(1)<\/code>\u65e0\u9700\u5224\u65ad\u5b57\u6bb5 <code>NULL<\/code>\uff0c\u4e14 MySQL \u4f1a\u9009\u6700\u4f18\u7d22\u5f15\uff1b<\/li>\n<li><code>count(\u5b57\u6bb5)<\/code>\u9700\u989d\u5916\u5224\u65ad <code>NULL<\/code>\uff0c\u975e\u7d22\u5f15\u5b57\u6bb5\u8fd8\u9700\u5168\u8868\u626b\u63cf\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>4. \u5b9e\u8df5\u5efa\u8bae<\/h2>\n<ul>\n<li><strong>\u7edf\u8ba1\u603b\u884c\u6570\uff1a<\/strong>\u4f18\u5148\u7528<code>count(*)<\/code>\uff08\u8bed\u4e49\u6700\u6e05\u6670\uff0cMySQL \u4f18\u5316\u6700\u597d\uff09\uff0c\u800c\u975e<code>count(1)<\/code>\u6216<code>count(\u4e3b\u952e)<\/code>\uff1b<\/li>\n<li><strong>\u7edf\u8ba1\u5b57\u6bb5\u975e <code>NULL<\/code> \u884c\u6570\uff1a<\/strong>\u82e5\u9700\u6b64\u903b\u8f91\uff0c\u786e\u4fdd\u5b57\u6bb5\u52a0\u7d22\u5f15\uff08\u5982<code>count(user_name)<\/code>\u9700<code>user_name<\/code>\u6709\u7d22\u5f15\uff09\uff1b<\/li>\n<li><strong>\u907f\u514d\u8bef\u533a\uff1a<\/strong>\n<ul>\n<li>\u8ba4\u4e3a<code>count(\u4e3b\u952e)<\/code>\u66f4\u5feb\uff1a\u5b9e\u9645<code>count(*)<\/code>\u4f1a\u9009\u66f4\u5c0f\u7684\u7d22\u5f15\uff0c\u6bd4\u4e3b\u952e\u7d22\u5f15\uff08\u805a\u96c6\u7d22\u5f15\uff09\u626b\u63cf\u66f4\u5feb\uff1b<\/li>\n<li>\u8ba4\u4e3a<code>count(1)<\/code>\u6bd4<code>count(*)<\/code>\u5feb\uff1aMySQL \u4f18\u5316\u540e\u4e24\u8005\u65e0\u533a\u522b\uff0c<code>count(*)<\/code>\u66f4\u7b26\u5408\u8bed\u4e49\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>5. \u603b\u7ed3<\/h2>\n<ul>\n<li>\u6838\u5fc3\u5dee\u5f02\uff1a<code>count(*)<\/code>\/<code>count(1)<\/code>\u7edf\u8ba1\u603b\u884c\u6570\uff08\u542b <code>NULL<\/code>\uff09\uff0c\u6027\u80fd\u6700\u4f18\uff1b<code>count(\u5b57\u6bb5)<\/code>\u7edf\u8ba1\u975e <code>NULL<\/code> \u884c\u6570\uff0c\u6027\u80fd\u8f83\u5dee\uff1b<\/li>\n<li>\u5b9e\u8df5\u9009\u62e9\uff1a\u7edf\u8ba1\u603b\u6570\u7528<code>count(*)<\/code>\uff0c\u7edf\u8ba1\u975e <code>NULL<\/code> \u5b57\u6bb5\u7528<code>count(\u7d22\u5f15\u5b57\u6bb5)<\/code>\uff1b<\/li>\n<li>\u4f18\u5316\u5173\u952e\uff1a\u5229\u7528 MySQL \u5bf9<code>count(*)<\/code>\u7684\u7d22\u5f15\u4f18\u5316\uff0c\u907f\u514d<code>count(\u975e\u7d22\u5f15\u5b57\u6bb5)<\/code>\u7684\u5168\u8868\u626b\u63cf\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<h2>6. \u6269\u5c55<\/h2>\n<p>\u7edf\u8ba1<code>user<\/code>\u8868<code>email<\/code>\u975e<code>null<\/code>\u7684\u603b\u884c\u6570\uff0c<code>email<\/code>\u662f\u666e\u901a\u7d22\u5f15\uff0c\u662f<code>SELECT COUNT(email) FROM user<\/code>\u5feb\uff0c\u8fd8\u662f<code>SELECT COUNT(*) FROM user WHERE email IS NOT NULL<\/code>\u5feb\uff1f<\/p>\n<p>\u7b54\uff1a\u57fa\u672c\u76f8\u540c\uff0c\u4f46\u662f\u4e0d\u63a8\u8350\u4f7f\u7528\u540e\u8005\uff0c\u63a8\u8350\u4f7f\u7528<code>COUNT(email)<\/code>\u3002\u56e0\u4e3a\u8fd9\u4e2a\u5168\u7a0b\u662f\u7d22\u5f15\uff0c\u867d\u7136\u9700\u8981\u7d22\u5f15\u626b\u63cf\uff0c\u4f46\u662f\u540e\u8005\u4f7f\u7528\u4e86<code>WHERE<\/code>\u6761\u4ef6\uff0c\u540c\u6837\u4e5f\u662f\u9700\u8981\u8fdb\u884c\u7d22\u5f15\u626b\u63cf\uff0c\u540c\u65f6\u8fd8\u591a\u51fa\u4e86\u4e00\u6b65\u6761\u4ef6\u8fc7\u6ee4\u3002\u540c\u65f6<code>COUNT(email)<\/code>\u8bed\u4e49\u66f4\u660e\u786e\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h1>09. <code>IN<\/code>\u548c<code>JOIN<\/code>\u54ea\u4e2a\u5feb<\/h1>\n<p><strong>\u5148\u660e\u786e\u4e24\u8005\u7684\u5e95\u5c42\u903b\u8f91<\/strong><\/p>\n<ul>\n<li><code>IN<\/code>\uff1a\u5c5e\u4e8e \u201c\u5b50\u67e5\u8be2 \/ \u503c\u5217\u8868\u5339\u914d\u201d\uff0c\u672c\u8d28\u662f\u5c06\u5b50\u67e5\u8be2\u7ed3\u679c\u52a0\u8f7d\u5230\u5185\u5b58\u540e\u505a\u5339\u914d\uff08\u6216\u76f4\u63a5\u5339\u914d\u503c\u5217\u8868\uff09\uff0c\u9002\u5408 \u201c\u5355\u5b57\u6bb5\u5339\u914d\u201d \u573a\u666f\uff1b<\/li>\n<li><code>JOIN<\/code>\uff1a\u5c5e\u4e8e \u201c\u591a\u8868\u5173\u8054\u201d\uff0c\u5e95\u5c42\u4ee5<code>Nested Loop Join<\/code>\u4e3a\u4e3b\uff08\u5c0f\u8868\u9a71\u52a8\u5927\u8868\uff09\uff0c\u901a\u8fc7\u5173\u8054\u5b57\u6bb5\u7d22\u5f15\u76f4\u63a5\u5339\u914d\uff0c\u9002\u5408 \u201c\u591a\u5b57\u6bb5\u5173\u8054\u53d6\u503c\u201d \u573a\u666f\u3002<\/li>\n<\/ul>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251119070927554.png'><img class=\"lazyload lazyload-style-6\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251119070927554.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<ul>\n<li><code>IN<\/code>\u7684\u4f18\u52bf\uff1a\u8bed\u6cd5\u7b80\u5355\uff0c\u9002\u5408 \u201c\u5355\u5b57\u6bb5\u5339\u914d\u201d \u7684\u7b80\u5355\u573a\u666f\uff1b<\/li>\n<li><code>JOIN<\/code>\u7684\u4f18\u52bf\uff1a\u652f\u6301\u591a\u5b57\u6bb5\u5173\u8054\uff0c\u5927\u7ed3\u679c\u96c6\u4e0b\u5229\u7528\u7d22\u5f15\u66f4\u9ad8\u6548\uff0c\u662f\u590d\u6742\u5173\u8054\u7684\u9996\u9009\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<p><a id=\"MySQL\u7684\u51fd\u6570\" class=\"offset-anchor\"><\/a><\/p>\n<h1>10. MySQL\u7684\u51fd\u6570<\/h1>\n<p>MySQL \u51fd\u6570\u6309\u529f\u80fd\u53ef\u5206\u4e3a<strong>\u5185\u7f6e\u51fd\u6570<\/strong>\uff08\u7cfb\u7edf\u63d0\u4f9b\uff0c\u91cd\u70b9\uff09\u548c<strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\uff08\u7528\u6237\u7f16\u5199\uff09\u3002<\/p>\n<div style=\"height:0.7em\"><\/div>\n<h2>1. \u6838\u5fc3\u5185\u7f6e\u51fd\u6570<\/h2>\n<h3>1. \u5b57\u7b26\u4e32\u51fd\u6570<\/h3>\n<p><strong>\u5904\u7406\u6587\u672c\u6570\u636e\u3002<\/strong><\/p>\n<p>\u7528\u4e8e\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u622a\u53d6\u3001\u66ff\u6362\u7b49\uff0c\u662f\u4e1a\u52a1\u4e2d\u6700\u5e38\u7528\u7684\u51fd\u6570\u7c7b\u522b\u3002<\/p>\n<ul>\n<li><code>CONCAT(str1, str2,...)<\/code>\uff1a\u5b57\u7b26\u4e32\u62fc\u63a5\uff08\u5982\uff1a\u62fc\u63a5\u7528\u6237\u59d3\u540d\u548c\u624b\u673a\u53f7\uff1a<code>CONCAT(username, &#039;-&#039;, mobile)<\/code>\uff09\uff1b<\/li>\n<li><code>SUBSTRING(str, pos, len)<\/code>\uff1a\u622a\u53d6\u5b50\u4e32\uff08\u5982\uff1a\u53d6\u624b\u673a\u53f7\u540e 4 \u4f4d\uff1a<code>SUBSTRING(mobile, 8, 4)<\/code>\uff09\uff1b<\/li>\n<li><code>REPLACE(str, old, new)<\/code>\uff1a\u66ff\u6362\u5b57\u7b26\u4e32\uff08\u5982\uff1a\u6e05\u7406\u5185\u5bb9\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff1a<code>REPLACE(content, &#039;&lt;&#039;, &#039;&#039;)<\/code>\uff09\uff1b<\/li>\n<li><code>LENGTH(str)<\/code>\uff1a\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\uff08\u5982\uff1a\u6821\u9a8c\u7528\u6237\u540d\u957f\u5ea6\uff1a<code>WHERE LENGTH(username) &gt; 6<\/code>\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u8df5\uff1a<\/strong>\u7528\u6237\u4fe1\u606f\u683c\u5f0f\u5316\u3001\u6587\u672c\u5185\u5bb9\u6e05\u6d17\u573a\u666f\u9ad8\u9891\u4f7f\u7528\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>2. \u6570\u503c\u51fd\u6570<\/h3>\n<p><strong>\u5904\u7406\u6570\u5b57\u8fd0\u7b97\u3002<\/strong><\/p>\n<p>\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u3001\u53d6\u6574\u3001\u8fdb\u5236\u8f6c\u6362\u7b49\u3002<\/p>\n<ul>\n<li><code>ROUND(num, n)<\/code>\uff1a\u56db\u820d\u4e94\u5165\uff08\u5982\uff1a\u91d1\u989d\u4fdd\u7559 2 \u4f4d\u5c0f\u6570\uff1a<code>ROUND(amount, 2)<\/code>\uff09\uff1b<\/li>\n<li><code>ABS(num)<\/code>\uff1a\u53d6\u7edd\u5bf9\u503c\uff08\u5982\uff1a\u8ba1\u7b97\u5dee\u503c\u7684\u7edd\u5bf9\u503c\uff1a<code>ABS(score1 - score2)<\/code>\uff09\uff1b<\/li>\n<li><code>CEIL(num)<\/code>\/<code>FLOOR(num)<\/code>\uff1a\u5411\u4e0a \/ \u5411\u4e0b\u53d6\u6574\uff08\u5982\uff1a\u8ba2\u5355\u6570\u91cf\u5411\u4e0a\u53d6\u6574\uff1a<code>CEIL(total\/10)<\/code>\uff09\uff1b<\/li>\n<li><code>SUM(num)<\/code>\/<code>AVG(num)<\/code>\uff1a\u6c42\u548c \/ \u5e73\u5747\u503c\uff08\u62a5\u8868\u7edf\u8ba1\u6838\u5fc3\u51fd\u6570\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u8df5\uff1a<\/strong>\u91d1\u989d\u8ba1\u7b97\u3001\u6570\u636e\u7edf\u8ba1\u573a\u666f\u5fc5\u5907\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>3. \u65e5\u671f\u65f6\u95f4\u51fd\u6570<\/h3>\n<p><strong>\u5904\u7406\u65f6\u95f4\u6570\u636e\u3002<\/strong><\/p>\n<p>\u7528\u4e8e\u65f6\u95f4\u83b7\u53d6\u3001\u683c\u5f0f\u5316\u3001\u5dee\u503c\u8ba1\u7b97\uff0c\u662f\u4e1a\u52a1\u4e2d\u4ec5\u6b21\u4e8e\u5b57\u7b26\u4e32\u51fd\u6570\u7684\u9ad8\u9891\u51fd\u6570\u3002<\/p>\n<ul>\n<li><code>NOW()<\/code>\/<code>CURDATE()<\/code>\uff1a\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff08\u542b\u65f6\u5206\u79d2\uff09\/ \u5f53\u524d\u65e5\u671f\uff08\u4ec5\u5e74\u6708\u65e5\uff09\uff1b<\/li>\n<li><code>DATE_FORMAT(date, fmt)<\/code>\uff1a\u65f6\u95f4\u683c\u5f0f\u5316\uff08\u5982\uff1a\u8ba2\u5355\u65f6\u95f4\u8f6c\u5b57\u7b26\u4e32\uff1a<code>DATE_FORMAT(create_time, &#039;%Y-%m-%d %H:%i:%s&#039;)<\/code>\uff09\uff1b<\/li>\n<li><code>DATEDIFF(date1, date2)<\/code>\uff1a\u8ba1\u7b97\u65e5\u671f\u5dee\u503c\uff08\u5982\uff1a\u7528\u6237\u6ce8\u518c\u5929\u6570\uff1a<code>DATEDIFF(NOW(), register_time)<\/code>\uff09\uff1b<\/li>\n<li><code>DATE_ADD(date, INTERVAL expr unit)<\/code>\uff1a\u65f6\u95f4\u589e\u51cf\uff08\u5982\uff1a\u8ba1\u7b97 7 \u5929\u540e\u7684\u65e5\u671f\uff1a<code>DATE_ADD(NOW(), INTERVAL 7 DAY)<\/code>\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u8df5\uff1a<\/strong>\u8ba2\u5355\u65f6\u95f4\u7b5b\u9009\u3001\u4f1a\u5458\u6709\u6548\u671f\u8ba1\u7b97\u3001\u62a5\u8868\u65f6\u95f4\u7ef4\u5ea6\u7edf\u8ba1\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>4. \u805a\u5408\u51fd\u6570<\/h3>\n<p><strong>\u6570\u636e\u7edf\u8ba1\u5206\u6790\u3002<\/strong><\/p>\n<p>\u7528\u4e8e\u5bf9\u591a\u884c\u6570\u636e\u8fdb\u884c\u805a\u5408\u8ba1\u7b97\uff0c\u9700\u7ed3\u5408<code>GROUP BY<\/code>\u4f7f\u7528\u3002<\/p>\n<ul>\n<li><code>COUNT(*)<\/code>\uff1a\u7edf\u8ba1\u603b\u884c\u6570\uff08\u7528\u6237\u6570\u3001\u8ba2\u5355\u6570\u7edf\u8ba1\uff09\uff1b<\/li>\n<li><code>SUM(field)<\/code>\uff1a\u6c42\u548c\uff08\u9500\u552e\u989d\u3001\u9500\u91cf\u7edf\u8ba1\uff09\uff1b<\/li>\n<li><code>MAX(field)<\/code>\/<code>MIN(field)<\/code>\uff1a\u6700\u5927\u503c \/ \u6700\u5c0f\u503c\uff08\u6700\u9ad8\u8ba2\u5355\u91d1\u989d\u3001\u6700\u65e9\u6ce8\u518c\u65f6\u95f4\uff09\uff1b<\/li>\n<li><code>GROUP_CONCAT(field)<\/code>\uff1a\u5206\u7ec4\u62fc\u63a5\u5b57\u7b26\u4e32\uff08\u5982\uff1a\u67e5\u8be2\u7528\u6237\u7684\u6240\u6709\u8ba2\u5355\u53f7\uff1a<code>GROUP_CONCAT(order_no)<\/code>\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u8df5\uff1a<\/strong>\u62a5\u8868\u7cfb\u7edf\u3001\u6570\u636e\u770b\u677f\u7684\u6838\u5fc3\u51fd\u6570\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>5. \u6761\u4ef6\u51fd\u6570<\/h3>\n<p><strong>\u903b\u8f91\u5224\u65ad\u3002<\/strong><\/p>\n<p>\u7528\u4e8e\u5b9e\u73b0 SQL \u4e2d\u7684\u6761\u4ef6\u5206\u652f\uff0c\u66ff\u4ee3\u590d\u6742\u7684WHERE\u5224\u65ad\u3002<\/p>\n<ul>\n<li><code>IF(condition, val1, val2)<\/code>\uff1a\u7b80\u5355\u6761\u4ef6\u5224\u65ad\uff08\u5982\uff1a\u5224\u65ad\u7528\u6237\u72b6\u6001\uff1a<code>IF(status=1, &#039;\u6b63\u5e38&#039;, &#039;\u7981\u7528&#039;)<\/code>\uff09\uff1b<\/li>\n<li><code>CASE WHEN<\/code>\uff1a\u590d\u6742\u6761\u4ef6\u5206\u652f\uff08\u5982\uff1a\u8ba2\u5355\u72b6\u6001\u6620\u5c04\uff1a<code>CASE status WHEN 0 THEN &#039;\u5f85\u652f\u4ed8&#039; WHEN 1 THEN &#039;\u5df2\u652f\u4ed8&#039; ELSE &#039;\u5df2\u53d6\u6d88&#039; END<\/code>\uff09\uff1b<\/li>\n<li><code>IFNULL(val, default)<\/code>\uff1a<code>NULL<\/code> \u503c\u66ff\u6362\uff08\u5982\uff1a\u7528\u6237\u6635\u79f0\u4e3a\u7a7a\u65f6\u663e\u793a\u9ed8\u8ba4\u503c\uff1a<code>IFNULL(nickname, &#039;\u6e38\u5ba2&#039;)<\/code>\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u8df5\uff1a<\/strong>\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5316\u3001\u52a8\u6001\u72b6\u6001\u5c55\u793a\u3002<\/p>\n<div style=\"height:1em\"><\/div>\n<h3>6. \u5176\u4ed6\u5e38\u7528\u51fd\u6570<\/h3>\n<ul>\n<li><code>UUID()<\/code>\uff1a\u751f\u6210\u552f\u4e00\u6807\u8bc6\u7b26\uff08\u5206\u5e03\u5f0f\u573a\u666f\u4e34\u65f6 ID \u751f\u6210\uff09\uff1b<\/li>\n<li><code>INSTR(str, substr)<\/code>\uff1a\u67e5\u627e\u5b50\u4e32\u4f4d\u7f6e\uff08\u5982\uff1a\u5224\u65ad\u5185\u5bb9\u662f\u5426\u542b\u5173\u952e\u8bcd\uff1a<code>INSTR(content, &#039;MySQL&#039;) &gt; 0<\/code>\uff09\uff1b<\/li>\n<li><code>CAST(val AS type)<\/code>\uff1a\u7c7b\u578b\u8f6c\u6362\uff08\u5982\uff1a\u5b57\u7b26\u4e32\u8f6c\u6570\u5b57\uff1a<code>CAST(score AS UNSIGNED)<\/code>\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h2>2. \u9762\u8bd5\u9ad8\u9891\u8003\u70b9<\/h2>\n<p><strong>\u51fd\u6570\u4f7f\u7528\u7684\u6027\u80fd\u9677\u9631\u3002<\/strong><\/p>\n<div style=\"height:1em\"><\/div>\n<p><strong>1. \u51fd\u6570\u64cd\u4f5c\u7d22\u5f15\u5b57\u6bb5\u4f1a\u5bfc\u81f4\u7d22\u5f15\u5931\u6548<\/strong><\/p>\n<p>\u8fd9\u662f\u9762\u8bd5\u5fc5\u95ee\u70b9\uff01\u82e5\u5bf9\u7d22\u5f15\u5b57\u6bb5\u4f7f\u7528\u51fd\u6570\uff0cMySQL \u65e0\u6cd5\u4f7f\u7528\u7d22\u5f15\uff0c\u4f1a\u89e6\u53d1\u5168\u8868\u626b\u63cf\u3002<\/p>\n<ul>\n<li><strong>\u53cd\u4f8b\uff1a<\/strong><code>WHERE DATE(create_time) = &#039;2024-01-01&#039;<\/code>\uff08\u5bf9\u7d22\u5f15\u5b57\u6bb5<code>create_time<\/code>\u7528<code>DATE()<\/code>\u51fd\u6570\uff0c\u7d22\u5f15\u5931\u6548\uff09\uff1b<\/li>\n<li><strong>\u6b63\u4f8b\uff1a<\/strong><code>WHERE create_time &gt;= &#039;2024-01-01&#039; AND create_time &lt; &#039;2024-01-02&#039;<\/code>\uff08\u76f4\u63a5\u5339\u914d\u5b57\u6bb5\u8303\u56f4\uff0c\u5229\u7528\u7d22\u5f15\uff09\u3002\u8fd9\u4e2a\u4e4b\u6240\u4ee5\u80fd\u5229\u7528\u7d22\u5f15\uff0c\u662f\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u51fd\u6570\u7834\u574f\u7d22\u5f15\u7684\u6709\u5e8f\u6027\uff0c\u540c\u65f6MySQL\u5c06\u7ed9\u51fa\u7684\u67e5\u8be2\u6761\u4ef6\u503c<code>&#039;2024-01-01&#039;<\/code>\u548c<code>&#039;2024-01-02&#039;<\/code>\u9690\u5f0f\u7684\u8f6c\u6362\u4e3a\u4e86<code>DATETIME<\/code>\u7c7b\u578b\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5c06\u6574\u4e2a\u8868\u7684<code>create_time<\/code>\u5b57\u6bb5\u8f6c\u6210\u5176\u4ed6\u7c7b\u578b\uff0c\u4fdd\u8bc1\u4e86\u539f\u7d22\u5f15\u7684\u53ef\u7528\u6027\u3002<\/li>\n<\/ul>\n<div style=\"height:1em\"><\/div>\n<p><strong>2. \u805a\u5408\u51fd\u6570\u7684NULL\u5904\u7406<\/strong><\/p>\n<ul>\n<li><code>COUNT(field)<\/code>\u4f1a\u5ffd\u7565 <code>NULL<\/code> \u503c\uff0c<code>COUNT(*)<\/code>\u7edf\u8ba1\u6240\u6709\u884c\uff08\u542b <code>NULL<\/code>\uff09\uff1b<\/li>\n<li><code>SUM(field)<\/code>\u4e2d <code>NULL<\/code> \u503c\u4f1a\u88ab\u89c6\u4e3a 0\uff0c\u4f46\u5efa\u8bae\u7528<code>IFNULL(field, 0)<\/code>\u663e\u5f0f\u5904\u7406\uff08\u907f\u514d\u8bed\u4e49\u6b67\u4e49\uff09\u3002<\/li>\n<\/ul>\n<p><strong>3. \u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u614e\u7528\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u81ea\u5b9a\u4e49\u51fd\u6570\uff08<code>CREATE FUNCTION<\/code>\uff09\u53ef\u5b9e\u73b0\u590d\u6742\u903b\u8f91\uff0c\u4f46\u6267\u884c\u6548\u7387\u4f4e\uff08\u65e0\u6cd5\u5e76\u884c\u6267\u884c\uff09\uff0c\u4e14\u53ef\u80fd\u5bfc\u81f4\u9501\u8868\uff0c\u9ad8\u5e76\u53d1\u573a\u666f\u5efa\u8bae\u7528\u5b58\u50a8\u8fc7\u7a0b\u6216\u5e94\u7528\u5c42\u4ee3\u7801\u66ff\u4ee3\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h2>3. \u603b\u7ed3<\/h2>\n<p>MySQL \u51fd\u6570\u662f\u63d0\u5347 SQL \u7075\u6d3b\u6027\u7684\u6838\u5fc3\u5de5\u5177\u3002<\/p>\n<ul>\n<li><strong>\u5206\u7c7b\u8bb0\u5fc6<\/strong>\uff1a\u91cd\u70b9\u638c\u63e1\u5b57\u7b26\u4e32\u3001\u65e5\u671f\u3001\u805a\u5408\u3001\u6761\u4ef6\u51fd\u6570\u7684\u5e38\u7528\u7528\u6cd5\uff1b<\/li>\n<li><strong>\u6027\u80fd\u9677\u9631<\/strong>\uff1a\u7d22\u5f15\u5b57\u6bb5\u907f\u514d\u4f7f\u7528\u51fd\u6570\uff0c\u5426\u5219\u7d22\u5f15\u5931\u6548\uff1b<\/li>\n<li><strong>\u5b9e\u8df5\u4f18\u5148<\/strong>\uff1a\u7b80\u5355\u903b\u8f91\u7528\u5185\u7f6e\u51fd\u6570\uff0c\u590d\u6742\u903b\u8f91\u4f18\u5148\u5e94\u7528\u5c42\u5904\u7406\uff08\u907f\u514d SQL \u81c3\u80bf\uff09\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<blockquote>\n<p>\u56de\u76ee\u5f55\uff1a <a href=\"https:\/\/www.tqazy.com\/?p=1972\" target=\"_blank\">\u300a\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u5173\u76ee\u5f55\u300b<\/a><br \/>\n\u4e0a\u4e00\u7bc7\uff1a <a href=\"https:\/\/www.tqazy.com\/?p=2082\" target=\"_blank\">\u300a\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517302 &#8211; \u7d22\u5f15\u300b<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u5b50\u67e5\u8be2\u4e0e\u8fde\u63a5\u67e5\u8be2\u3001\u5b50\u67e5\u8be2\u4e0ejoin\u6027\u80fd\u5bf9\u6bd4\u53ca\u9002\u7528\u573a\u666f\u3001\u590d\u6742\u67e5\u8be2\uff08group by\/having\u3001limit\u5206\u9875\uff09\u7684\u6267\u884c\u903b\u8f91\u3001order by \u7684\u6392\u5e8f\u539f\u7406\u3001\u6162\u67e5\u8be2\u7684\u5b9a\u4f4d\u4e0e\u5206\u6790\u3001\u590d\u6742SQL\u7684\u62c6\u5206\u4e0e\u6539\u5199\uff08\u5927\u8868\u5206\u9875\u4f18\u5316\uff1a\u5ef6\u8fdf\u5173\u8054\u3001\u4e66\u7b7e\u5206\u9875\uff09\u3001join\u4f18\u5316\uff08\u5c0f\u8868\u9a71\u52a8\u5927\u8868\u3001 \u907f\u514d cross join\u3001count\u7684\u6027\u80fd\u5dee\u5f02\uff08count(1)\u3001count(*)\u3001count(\u5b57\u6bb5)\uff09\u3001IN\u548cJOIN\u54ea\u4e2a\u5feb\u3001MySQL\u7684\u51fd\u6570\u3002\u672c\u6587\u4f1a\u6301\u7eed\u66f4\u65b0&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[109,85],"class_list":["post-2087","post","type-post","status-publish","format-standard","hentry","category-interview","tag-mysql","tag-85"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.tqazy.com\/?p=2087\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"\u5b50\u67e5\u8be2\u4e0e\u8fde\u63a5\u67e5\u8be2\u3001\u5b50\u67e5\u8be2\u4e0ejoin\u6027\u80fd\u5bf9\u6bd4\u53ca\u9002\u7528\u573a\u666f\u3001\u590d\u6742\u67e5\u8be2\uff08group by\/having\u3001limit\u5206\u9875\uff09\u7684\u6267\u884c\u903b\u8f91\u3001order by \u7684\u6392\u5e8f\u539f\u7406\u3001\u6162\u67e5\u8be2\u7684\u5b9a\u4f4d\u4e0e\u5206\u6790\u3001\u590d\u6742SQL\u7684\u62c6\u5206\u4e0e\u6539\u5199\uff08\u5927\u8868\u5206\u9875\u4f18\u5316\uff1a\u5ef6\u8fdf\u5173\u8054\u3001\u4e66\u7b7e\u5206\u9875\uff09\u3001join\u4f18\u5316\uff08\u5c0f\u8868\u9a71\u52a8\u5927\u8868\u3001 \u907f\u514d cross join\u3001count\u7684\u6027\u80fd\u5dee\u5f02\uff08count(1)\u3001count(*)\u3001count(\u5b57\u6bb5)\uff09\u3001IN\u548cJOIN\u54ea\u4e2a\u5feb\u3001MySQL\u7684\u51fd\u6570\u3002\u672c\u6587\u4f1a\u6301\u7eed\u66f4\u65b0...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.tqazy.com\/?p=2087\" \/>\n<meta property=\"og:site_name\" content=\"\u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2025-11-20T05:03:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-20T07:08:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png\" \/>\n<meta name=\"author\" content=\"\u9676\u5176\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u9676\u5176\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.tqazy.com\/?p=2087#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=2087\"},\"author\":{\"name\":\"\u9676\u5176\",\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e\"},\"headline\":\"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316\",\"datePublished\":\"2025-11-20T05:03:42+00:00\",\"dateModified\":\"2025-11-20T07:08:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=2087\"},\"wordCount\":126,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e\"},\"image\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=2087#primaryimage\"},\"thumbnailUrl\":\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png\",\"keywords\":[\"mysql\",\"\u9762\u8bd5\u516b\u80a1\"],\"articleSection\":[\"\u9762\u8bd5\u516b\u80a1\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.tqazy.com\/?p=2087#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tqazy.com\/?p=2087\",\"url\":\"https:\/\/www.tqazy.com\/?p=2087\",\"name\":\"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.tqazy.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=2087#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=2087#primaryimage\"},\"thumbnailUrl\":\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png\",\"datePublished\":\"2025-11-20T05:03:42+00:00\",\"dateModified\":\"2025-11-20T07:08:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=2087#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.tqazy.com\/?p=2087\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.tqazy.com\/?p=2087#primaryimage\",\"url\":\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png\",\"contentUrl\":\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.tqazy.com\/?p=2087#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.tqazy.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.tqazy.com\/#website\",\"url\":\"https:\/\/www.tqazy.com\/\",\"name\":\"\u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2\",\"description\":\"\u5f53\u4e0b\uff0c\u5373\u662f\u672a\u6765\",\"publisher\":{\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e\"},\"alternateName\":\"TaoQi's Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.tqazy.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e\",\"name\":\"\u9676\u5176\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/06\/20240604021741672.jpg\",\"contentUrl\":\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/06\/20240604021741672.jpg\",\"width\":940,\"height\":940,\"caption\":\"\u9676\u5176\"},\"logo\":{\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/image\/\"},\"description\":\"\u4e00\u4e2a\u5f88\u61d2\u7684\u7a0b\u5e8f\u5458\",\"sameAs\":[\"https:\/\/www.tqazy.com\"],\"url\":\"https:\/\/www.tqazy.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.tqazy.com\/?p=2087","og_locale":"zh_CN","og_type":"article","og_title":"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","og_description":"\u5b50\u67e5\u8be2\u4e0e\u8fde\u63a5\u67e5\u8be2\u3001\u5b50\u67e5\u8be2\u4e0ejoin\u6027\u80fd\u5bf9\u6bd4\u53ca\u9002\u7528\u573a\u666f\u3001\u590d\u6742\u67e5\u8be2\uff08group by\/having\u3001limit\u5206\u9875\uff09\u7684\u6267\u884c\u903b\u8f91\u3001order by \u7684\u6392\u5e8f\u539f\u7406\u3001\u6162\u67e5\u8be2\u7684\u5b9a\u4f4d\u4e0e\u5206\u6790\u3001\u590d\u6742SQL\u7684\u62c6\u5206\u4e0e\u6539\u5199\uff08\u5927\u8868\u5206\u9875\u4f18\u5316\uff1a\u5ef6\u8fdf\u5173\u8054\u3001\u4e66\u7b7e\u5206\u9875\uff09\u3001join\u4f18\u5316\uff08\u5c0f\u8868\u9a71\u52a8\u5927\u8868\u3001 \u907f\u514d cross join\u3001count\u7684\u6027\u80fd\u5dee\u5f02\uff08count(1)\u3001count(*)\u3001count(\u5b57\u6bb5)\uff09\u3001IN\u548cJOIN\u54ea\u4e2a\u5feb\u3001MySQL\u7684\u51fd\u6570\u3002\u672c\u6587\u4f1a\u6301\u7eed\u66f4\u65b0...","og_url":"https:\/\/www.tqazy.com\/?p=2087","og_site_name":"\u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","article_published_time":"2025-11-20T05:03:42+00:00","article_modified_time":"2025-11-20T07:08:37+00:00","og_image":[{"url":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png","type":"","width":"","height":""}],"author":"\u9676\u5176","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u9676\u5176","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tqazy.com\/?p=2087#article","isPartOf":{"@id":"https:\/\/www.tqazy.com\/?p=2087"},"author":{"name":"\u9676\u5176","@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e"},"headline":"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316","datePublished":"2025-11-20T05:03:42+00:00","dateModified":"2025-11-20T07:08:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.tqazy.com\/?p=2087"},"wordCount":126,"commentCount":3,"publisher":{"@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e"},"image":{"@id":"https:\/\/www.tqazy.com\/?p=2087#primaryimage"},"thumbnailUrl":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png","keywords":["mysql","\u9762\u8bd5\u516b\u80a1"],"articleSection":["\u9762\u8bd5\u516b\u80a1"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.tqazy.com\/?p=2087#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.tqazy.com\/?p=2087","url":"https:\/\/www.tqazy.com\/?p=2087","name":"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.tqazy.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.tqazy.com\/?p=2087#primaryimage"},"image":{"@id":"https:\/\/www.tqazy.com\/?p=2087#primaryimage"},"thumbnailUrl":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png","datePublished":"2025-11-20T05:03:42+00:00","dateModified":"2025-11-20T07:08:37+00:00","breadcrumb":{"@id":"https:\/\/www.tqazy.com\/?p=2087#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.tqazy.com\/?p=2087"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.tqazy.com\/?p=2087#primaryimage","url":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png","contentUrl":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2025\/11\/20251118050421491.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tqazy.com\/?p=2087#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.tqazy.com\/"},{"@type":"ListItem","position":2,"name":"\u9762\u8bd5\u7b14\u8bb0\uff1aMySQL \u76f8\u517303 \u2013 SQL\u8bed\u6cd5\u4e0e\u67e5\u8be2\u4f18\u5316"}]},{"@type":"WebSite","@id":"https:\/\/www.tqazy.com\/#website","url":"https:\/\/www.tqazy.com\/","name":"\u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","description":"\u5f53\u4e0b\uff0c\u5373\u662f\u672a\u6765","publisher":{"@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e"},"alternateName":"TaoQi's Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.tqazy.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e","name":"\u9676\u5176","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/image\/","url":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/06\/20240604021741672.jpg","contentUrl":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/06\/20240604021741672.jpg","width":940,"height":940,"caption":"\u9676\u5176"},"logo":{"@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/image\/"},"description":"\u4e00\u4e2a\u5f88\u61d2\u7684\u7a0b\u5e8f\u5458","sameAs":["https:\/\/www.tqazy.com"],"url":"https:\/\/www.tqazy.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/posts\/2087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2087"}],"version-history":[{"count":3,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/posts\/2087\/revisions"}],"predecessor-version":[{"id":2113,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/posts\/2087\/revisions\/2113"}],"wp:attachment":[{"href":"https:\/\/www.tqazy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}