{"id":168449,"date":"2024-11-06T12:28:35","date_gmt":"2024-11-06T04:28:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168449\/"},"modified":"2024-11-06T12:28:35","modified_gmt":"2024-11-06T04:28:35","slug":"sql-%e8%aa%9e%e5%8f%a5%e7%9a%84-22-%e6%a2%9d%e6%b3%95%e5%af%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168449\/","title":{"rendered":"SQL \u8a9e\u53e5\u7684 22 \u689d\u6cd5\u5bf6"},"content":{"rendered":"<h1 id=\"sql-%e8%aa%9e%e5%8f%a5%e7%9a%84-22-%e6%a2%9d%e6%b3%95%e5%af%b6-byEtiuMXqO\">SQL \u8a9e\u53e5\u7684 22 \u689d\u6cd5\u5bf6<\/h1>\n<p>SQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u7528\u65bc\u7ba1\u7406\u548c\u64cd\u4f5c\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u6a19\u6e96\u8a9e\u8a00\u3002\u7121\u8ad6\u662f\u6578\u64da\u7684\u67e5\u8a62\u3001\u66f4\u65b0\u9084\u662f\u7ba1\u7406\uff0cSQL \u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u672c\u6587\u5c07\u4ecb\u7d39 22 \u689d SQL \u8a9e\u53e5\u7684\u6cd5\u5bf6\uff0c\u5e6b\u52a9\u60a8\u66f4\u6709\u6548\u5730\u4f7f\u7528 SQL\u3002<\/p>\n<h2 id=\"1-select-%e8%aa%9e%e5%8f%a5-byEtiuMXqO\">1. SELECT \u8a9e\u53e5<\/h2>\n<p>SELECT \u8a9e\u53e5\u662f SQL \u7684\u57fa\u790e\uff0c\u7528\u65bc\u5f9e\u6578\u64da\u5eab\u4e2d\u6aa2\u7d22\u6578\u64da\u3002<\/p>\n<pre><code>SELECT * FROM users;<\/code><\/pre>\n<h2 id=\"2-where-%e5%ad%90%e5%8f%a5-byEtiuMXqO\">2. WHERE \u5b50\u53e5<\/h2>\n<p>WHERE \u5b50\u53e5\u7528\u65bc\u904e\u6ffe\u67e5\u8a62\u7d50\u679c\uff0c\u50c5\u8fd4\u56de\u7b26\u5408\u689d\u4ef6\u7684\u8a18\u9304\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE age &gt; 18;<\/code><\/pre>\n<h2 id=\"3-order-by-%e5%ad%90%e5%8f%a5-byEtiuMXqO\">3. ORDER BY \u5b50\u53e5<\/h2>\n<p>ORDER BY \u5b50\u53e5\u7528\u65bc\u5c0d\u67e5\u8a62\u7d50\u679c\u9032\u884c\u6392\u5e8f\u3002<\/p>\n<pre><code>SELECT * FROM users ORDER BY name ASC;<\/code><\/pre>\n<h2 id=\"4-group-by-%e5%ad%90%e5%8f%a5-byEtiuMXqO\">4. GROUP BY \u5b50\u53e5<\/h2>\n<p>GROUP BY \u5b50\u53e5\u7528\u65bc\u5c07\u7d50\u679c\u96c6\u6309\u4e00\u500b\u6216\u591a\u500b\u5217\u9032\u884c\u5206\u7d44\u3002<\/p>\n<pre><code>SELECT age, COUNT(*) FROM users GROUP BY age;<\/code><\/pre>\n<h2 id=\"5-having-%e5%ad%90%e5%8f%a5-byEtiuMXqO\">5. HAVING \u5b50\u53e5<\/h2>\n<p>HAVING \u5b50\u53e5\u7528\u65bc\u5c0d GROUP BY \u7684\u7d50\u679c\u9032\u884c\u904e\u6ffe\u3002<\/p>\n<pre><code>SELECT age, COUNT(*) FROM users GROUP BY age HAVING COUNT(*) &gt; 1;<\/code><\/pre>\n<h2 id=\"6-join-%e8%aa%9e%e5%8f%a5-byEtiuMXqO\">6. JOIN \u8a9e\u53e5<\/h2>\n<p>JOIN \u8a9e\u53e5\u7528\u65bc\u5c07\u591a\u500b\u8868\u4e2d\u7684\u6578\u64da\u9032\u884c\u806f\u63a5\u3002<\/p>\n<pre><code>SELECT users.name, orders.amount FROM users JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n<h2 id=\"7-inner-join-byEtiuMXqO\">7. INNER JOIN<\/h2>\n<p>INNER JOIN \u53ea\u8fd4\u56de\u5169\u500b\u8868\u4e2d\u5339\u914d\u7684\u8a18\u9304\u3002<\/p>\n<pre><code>SELECT * FROM users INNER JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n<h2 id=\"8-left-join-byEtiuMXqO\">8. LEFT JOIN<\/h2>\n<p>LEFT JOIN \u8fd4\u56de\u5de6\u8868\u7684\u6240\u6709\u8a18\u9304\uff0c\u5373\u4f7f\u53f3\u8868\u4e2d\u6c92\u6709\u5339\u914d\u7684\u8a18\u9304\u3002<\/p>\n<pre><code>SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n<h2 id=\"9-right-join-byEtiuMXqO\">9. RIGHT JOIN<\/h2>\n<p>RIGHT JOIN \u8fd4\u56de\u53f3\u8868\u7684\u6240\u6709\u8a18\u9304\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u6c92\u6709\u5339\u914d\u7684\u8a18\u9304\u3002<\/p>\n<pre><code>SELECT * FROM users RIGHT JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n<h2 id=\"10-full-outer-join-byEtiuMXqO\">10. FULL OUTER JOIN<\/h2>\n<p>FULL OUTER JOIN \u8fd4\u56de\u5169\u500b\u8868\u4e2d\u7684\u6240\u6709\u8a18\u9304\uff0c\u7121\u8ad6\u662f\u5426\u6709\u5339\u914d\u3002<\/p>\n<pre><code>SELECT * FROM users FULL OUTER JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n<h2 id=\"11-union-byEtiuMXqO\">11. UNION<\/h2>\n<p>UNION \u7528\u65bc\u5408\u4f75\u5169\u500b\u6216\u591a\u500b SELECT \u8a9e\u53e5\u7684\u7d50\u679c\u96c6\u3002<\/p>\n<pre><code>SELECT name FROM users UNION SELECT name FROM admins;<\/code><\/pre>\n<h2 id=\"12-insert-%e8%aa%9e%e5%8f%a5-byEtiuMXqO\">12. INSERT \u8a9e\u53e5<\/h2>\n<p>INSERT \u8a9e\u53e5\u7528\u65bc\u5411\u8868\u4e2d\u63d2\u5165\u65b0\u8a18\u9304\u3002<\/p>\n<pre><code>INSERT INTO users (name, age) VALUES ('John', 30);<\/code><\/pre>\n<h2 id=\"13-update-%e8%aa%9e%e5%8f%a5-byEtiuMXqO\">13. UPDATE \u8a9e\u53e5<\/h2>\n<p>UPDATE \u8a9e\u53e5\u7528\u65bc\u66f4\u65b0\u8868\u4e2d\u7684\u73fe\u6709\u8a18\u9304\u3002<\/p>\n<pre><code>UPDATE users SET age = 31 WHERE name = 'John';<\/code><\/pre>\n<h2 id=\"14-delete-%e8%aa%9e%e5%8f%a5-byEtiuMXqO\">14. DELETE \u8a9e\u53e5<\/h2>\n<p>DELETE \u8a9e\u53e5\u7528\u65bc\u522a\u9664\u8868\u4e2d\u7684\u8a18\u9304\u3002<\/p>\n<pre><code>DELETE FROM users WHERE name = 'John';<\/code><\/pre>\n<h2 id=\"15-distinct-%e9%97%9c%e9%8d%b5%e5%ad%97-byEtiuMXqO\">15. DISTINCT \u95dc\u9375\u5b57<\/h2>\n<p>DISTINCT \u7528\u65bc\u8fd4\u56de\u552f\u4e00\u7684\u503c\u3002<\/p>\n<pre><code>SELECT DISTINCT age FROM users;<\/code><\/pre>\n<h2 id=\"16-like-%e6%93%8d%e4%bd%9c%e7%ac%a6-byEtiuMXqO\">16. LIKE \u64cd\u4f5c\u7b26<\/h2>\n<p>LIKE \u64cd\u4f5c\u7b26\u7528\u65bc\u5728 WHERE \u5b50\u53e5\u4e2d\u9032\u884c\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE name LIKE 'J%';<\/code><\/pre>\n<h2 id=\"17-in-%e6%93%8d%e4%bd%9c%e7%ac%a6-byEtiuMXqO\">17. IN \u64cd\u4f5c\u7b26<\/h2>\n<p>IN \u64cd\u4f5c\u7b26\u7528\u65bc\u6aa2\u67e5\u67d0\u500b\u503c\u662f\u5426\u5728\u4e00\u7d44\u503c\u4e2d\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE age IN (18, 19, 20);<\/code><\/pre>\n<h2 id=\"18-between-%e6%93%8d%e4%bd%9c%e7%ac%a6-byEtiuMXqO\">18. BETWEEN \u64cd\u4f5c\u7b26<\/h2>\n<p>BETWEEN \u64cd\u4f5c\u7b26\u7528\u65bc\u7bc4\u570d\u67e5\u8a62\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE age BETWEEN 18 AND 25;<\/code><\/pre>\n<h2 id=\"19-null-%e5%80%bc%e8%99%95%e7%90%86-byEtiuMXqO\">19. NULL \u503c\u8655\u7406<\/h2>\n<p>\u4f7f\u7528 IS NULL \u6216 IS NOT NULL \u4f86\u6aa2\u67e5 NULL \u503c\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE age IS NULL;<\/code><\/pre>\n<h2 id=\"20-%e5%ad%90%e6%9f%a5%e8%a9%a2-byEtiuMXqO\">20. \u5b50\u67e5\u8a62<\/h2>\n<p>\u5b50\u67e5\u8a62\u662f\u5d4c\u5957\u5728\u5176\u4ed6\u67e5\u8a62\u4e2d\u7684\u67e5\u8a62\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE age &gt; (SELECT AVG(age) FROM users);<\/code><\/pre>\n<h2 id=\"21-%e4%ba%8b%e5%8b%99%e6%8e%a7%e5%88%b6-byEtiuMXqO\">21. \u4e8b\u52d9\u63a7\u5236<\/h2>\n<p>\u4f7f\u7528\u4e8b\u52d9\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<pre><code>BEGIN; UPDATE users SET age = 31 WHERE name = 'John'; COMMIT;<\/code><\/pre>\n<h2 id=\"22-%e7%b4%a2%e5%bc%95%e7%9a%84%e4%bd%bf%e7%94%a8-byEtiuMXqO\">22. \u7d22\u5f15\u7684\u4f7f\u7528<\/h2>\n<p>\u7d22\u5f15\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\uff0c\u4f46\u4e5f\u6703\u5f71\u97ff\u5beb\u5165\u6027\u80fd\u3002\u5408\u7406\u4f7f\u7528\u7d22\u5f15\u662f\u512a\u5316\u6578\u64da\u5eab\u6027\u80fd\u7684\u95dc\u9375\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-byEtiuMXqO\">\u7e3d\u7d50<\/h2>\n<p>\u638c\u63e1 SQL \u8a9e\u53e5\u7684\u9019 22 \u689d\u6cd5\u5bf6\uff0c\u80fd\u5920\u5e6b\u52a9\u60a8\u66f4\u9ad8\u6548\u5730\u9032\u884c\u6578\u64da\u64cd\u4f5c\u548c\u7ba1\u7406\u3002\u7121\u8ad6\u60a8\u662f\u6578\u64da\u5eab\u7ba1\u7406\u54e1\u9084\u662f\u958b\u767c\u8005\uff0c\u9019\u4e9b\u6280\u5de7\u90fd\u5c07\u4f7f\u60a8\u7684\u5de5\u4f5c\u66f4\u52a0\u5f97\u5fc3\u61c9\u624b\u3002\u5982\u679c\u60a8\u9700\u8981\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u61c9\u7528\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\uff0c\u8b93\u60a8\u7684\u696d\u52d9\u904b\u884c\u66f4\u52a0\u9806\u66a2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1 SQL \u8a9e\u53e5\u7684 22 \u689d\u6cd5\u5bf6\uff0c\u63d0\u5347\u6578\u64da\u67e5\u8a62\u6548\u7387\uff0c\u512a\u5316\u8cc7\u6599\u5eab\u64cd\u4f5c\uff0c\u8b93\u4f60\u7684 SQL \u6280\u80fd\u66f4\u4e0a\u4e00\u5c64\u6a13\uff01<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-168449","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=168449"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168449\/revisions"}],"predecessor-version":[{"id":168450,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168449\/revisions\/168450"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}