{"id":5215,"date":"2024-11-14T13:00:31","date_gmt":"2024-11-14T05:00:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5215\/"},"modified":"2024-11-14T13:00:31","modified_gmt":"2024-11-14T05:00:31","slug":"sql%e5%ad%90%e6%9f%a5%e8%af%a2%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5215\/","title":{"rendered":"sql\u5b50\u67e5\u8be2\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  sql \u5b50\u67e5\u8be2\u662f\u5d4c\u5957\u5728\u4e3b\u67e5\u8be2\u4e2d\u7684\u72ec\u7acb\u67e5\u8be2\uff0c\u7528\u4e8e\u6267\u884c\u591a\u4e2a\u67e5\u8be2\u3002\u7c7b\u578b\u6709\uff1a\u5173\u8054\u5b50\u67e5\u8be2\uff1a\u5173\u8054\u4e3b\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u884c\u3002\u975e\u5173\u8054\u5b50\u67e5\u8be2\uff1a\u4e0d\u5173\u8054\u4e3b\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u884c\u3002\u76f8\u5173\u5b50\u67e5\u8be2\uff1a\u4ece\u4e3b\u67e5\u8be2\u5f15\u7528\u5217\u3002\u7528\u6cd5\u5305\u62ec\uff1a\u8fc7\u6ee4\u6570\u636e\u3001\u805a\u5408\u6570\u636e\u3001\u6bd4\u8f83\u6570\u636e\u3001\u66f4\u65b0\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052421242323501.jpg\" class=\"aligncenter\" title=\"sql\u5b50\u67e5\u8be2\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u5b50\u67e5\u8be2\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5b50\u67e5\u8be2\u6307\u5357<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f SQL \u5b50\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>SQL \u5b50\u67e5\u8be2\u662f\u4e00\u4e2a\u5d4c\u5957\u5728\u4e3b\u67e5\u8be2\u4e2d\u7684\u72ec\u7acb\u67e5\u8be2\u3002\u5b83\u5141\u8bb8\u5728\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u4e2d\u6267\u884c\u591a\u4e2a\u67e5\u8be2\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 SQL \u5b50\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u5b50\u67e5\u8be2\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>\u4e3b\u67e5\u8be2\n(\u5b50\u67e5\u8be2)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT *\nFROM users\nWHERE id IN (\n    SELECT user_id\n    FROM orders\n    WHERE product_id = 1\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5b50\u67e5\u8be2\u7684\u7c7b\u578b<\/strong><\/p>\n<p>\u6709\u4e09\u79cd\u7c7b\u578b\u7684\u5b50\u67e5\u8be2\uff1a<\/p>\n<ul>\n<li> <strong>\u5173\u8054\u5b50\u67e5\u8be2\uff1a<\/strong>\u5173\u8054\u4e3b\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u4e2d\u7684\u884c\u3002<\/li>\n<li> <strong>\u975e\u5173\u8054\u5b50\u67e5\u8be2\uff1a<\/strong>\u4e0d\u5173\u8054\u4e3b\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u4e2d\u7684\u884c\u3002<\/li>\n<li> <strong>\u76f8\u5173\u5b50\u67e5\u8be2\uff1a<\/strong>\u4ece\u4e3b\u67e5\u8be2\u4e2d\u5f15\u7528\u5217\u3002<\/li>\n<\/ul>\n<p><strong>\u5b50\u67e5\u8be2\u7684\u7528\u6cd5<\/strong><\/p>\n<p>\u5b50\u67e5\u8be2\u53ef\u4ee5\u7528\u4e8e\uff1a<\/p>\n<ul>\n<li> <strong>\u8fc7\u6ee4\u6570\u636e\uff1a<\/strong>\u4f7f\u7528\u5b50\u67e5\u8be2\u4ece\u4e3b\u8868\u4e2d\u8fc7\u6ee4\u6570\u636e\u3002<\/li>\n<li> <strong>\u805a\u5408\u6570\u636e\uff1a<\/strong>\u83b7\u53d6\u5b50\u67e5\u8be2\u4e2d\u6570\u636e\u7684\u805a\u5408\u7ed3\u679c\uff08\u4f8b\u5982\uff0cSUM\u3001COUNT\uff09\u3002<\/li>\n<li> <strong>\u6bd4\u8f83\u6570\u636e\uff1a<\/strong>\u6bd4\u8f83\u5b50\u67e5\u8be2\u548c\u4e3b\u67e5\u8be2\u4e2d\u7684\u6570\u636e\u3002<\/li>\n<li> <strong>\u66f4\u65b0\u6570\u636e\uff1a<\/strong>\u4f7f\u7528\u5b50\u67e5\u8be2\u66f4\u65b0\u4e3b\u8868\u4e2d\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p><strong>\u5173\u8054\u5b50\u67e5\u8be2<\/strong><\/p>\n<pre>SELECT *\nFROM users\nWHERE id IN (\n    SELECT user_id\n    FROM orders\n    WHERE product_id = 1\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u975e\u5173\u8054\u5b50\u67e5\u8be2<\/strong><\/p>\n<pre>SELECT *\nFROM users\nWHERE email LIKE '%@example.com%'\nAND user_id &gt; (\n    SELECT MIN(user_id)\n    FROM users\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u76f8\u5173\u5b50\u67e5\u8be2<\/strong><\/p>\n<pre>UPDATE users\nSET last_login = CURRENT_TIMESTAMP\nWHERE id = (\n    SELECT user_id\n    FROM orders\n    WHERE product_id = 1\n    ORDER BY order_date DESC\n    LIMIT 1\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5b50\u67e5\u8be2\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sql \u5b50\u67e5\u8be2\u662f\u5d4c\u5957\u5728\u4e3b\u67e5\u8be2\u4e2d\u7684\u72ec\u7acb\u67e5\u8be2\uff0c\u7528\u4e8e\u6267\u884c\u591a\u4e2a\u67e5\u8be2\u3002\u7c7b\u578b\u6709\uff1a\u5173\u8054\u5b50\u67e5\u8be2\uff1a\u5173\u8054\u4e3b\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u884c\u3002\u975e\u5173\u8054\u5b50\u67e5\u8be2\uff1a\u4e0d\u5173\u8054\u4e3b\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u884c\u3002\u76f8\u5173\u5b50\u67e5\u8be2\uff1a\u4ece\u4e3b\u67e5\u8be2\u5f15\u7528\u5217\u3002\u7528\u6cd5\u5305\u62ec\uff1a\u8fc7\u6ee4\u6570\u636e\u3001\u805a\u5408\u6570\u636e\u3001\u6bd4\u8f83\u6570\u636e\u3001\u66f4\u65b0\u6570\u636e\u3002 SQL \u5b50\u67e5\u8be2\u6307\u5357 \u4ec0\u4e48\u662f SQL \u5b50\u67e5\u8be2\uff1f SQL \u5b50\u67e5\u8be2\u662f\u4e00\u4e2a\u5d4c\u5957\u5728\u4e3b\u67e5\u8be2\u4e2d\u7684\u72ec\u7acb\u67e5\u8be2\u3002\u5b83\u5141\u8bb8\u5728\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u4e2d\u6267\u884c\u591a\u4e2a\u67e5\u8be2\u3002 \u5982\u4f55\u4f7f\u7528 SQL \u5b50\u67e5\u8be2\uff1f \u5b50\u67e5\u8be2\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a \u4e3b\u67e5\u8be2 (\u5b50\u67e5\u8be2) \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff1a SELECT * FROM users WHERE id IN ( SELECT user_id FROM orders WHERE product_id = 1 ); \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u5b50\u67e5\u8be2\u7684\u7c7b\u578b \u6709\u4e09\u79cd\u7c7b\u578b\u7684\u5b50\u67e5\u8be2\uff1a \u5173\u8054\u5b50\u67e5\u8be2\uff1a\u5173\u8054\u4e3b\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u4e2d\u7684\u884c\u3002 \u975e\u5173\u8054\u5b50\u67e5\u8be2\uff1a\u4e0d\u5173\u8054\u4e3b\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u4e2d\u7684\u884c\u3002 \u76f8\u5173\u5b50\u67e5\u8be2\uff1a\u4ece\u4e3b\u67e5\u8be2\u4e2d\u5f15\u7528\u5217\u3002 \u5b50\u67e5\u8be2\u7684\u7528\u6cd5 \u5b50\u67e5\u8be2\u53ef\u4ee5\u7528\u4e8e\uff1a \u8fc7\u6ee4\u6570\u636e\uff1a\u4f7f\u7528\u5b50\u67e5\u8be2\u4ece\u4e3b\u8868\u4e2d\u8fc7\u6ee4\u6570\u636e\u3002 \u805a\u5408\u6570\u636e\uff1a\u83b7\u53d6\u5b50\u67e5\u8be2\u4e2d\u6570\u636e\u7684\u805a\u5408\u7ed3\u679c\uff08\u4f8b\u5982\uff0cSUM\u3001COUNT\uff09\u3002 \u6bd4\u8f83\u6570\u636e\uff1a\u6bd4\u8f83\u5b50\u67e5\u8be2\u548c\u4e3b\u67e5\u8be2\u4e2d\u7684\u6570\u636e\u3002 \u66f4\u65b0\u6570\u636e\uff1a\u4f7f\u7528\u5b50\u67e5\u8be2\u66f4\u65b0\u4e3b\u8868\u4e2d\u7684\u6570\u636e\u3002 \u793a\u4f8b \u5173\u8054\u5b50\u67e5\u8be2 SELECT * FROM users WHERE id [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5215","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5215"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5215\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}