{"id":116,"date":"2023-11-25T17:32:00","date_gmt":"2023-11-25T09:32:00","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=116"},"modified":"2024-04-01T17:33:01","modified_gmt":"2024-04-01T09:33:01","slug":"sql-%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2-%e5%ad%90%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=116","title":{"rendered":"SQL-\u591a\u8868\u67e5\u8be2-\u5b50\u67e5\u8be2"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"># \u5b50\u67e5\u8be2\u4ecb\u7ecd<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6982\u5ff5\uff1aSQL\u8bed\u53e5\u4e2d\u5d4c\u5957SELECT\u8bed\u53e5\uff0c\u79f0\u8c13\u5d4c\u5957\u67e5\u8be2\uff0c\u53c8\u79f0\u5b50\u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>SELECT * FROM t1 WHERE column1 = ( SELECT column1 FROM t2);<\/code><\/pre>\n<\/blockquote>\n\n\n\n<p>\u5b50\u67e5\u8be2\u5916\u90e8\u7684\u8bed\u53e5\u53ef\u4ee5\u662f INSERT \/ UPDATE \/ DELETE \/ SELECT \u7684\u4efb\u4f55\u4e00\u4e2a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6839\u636e\u5b50\u67e5\u8be2\u7ed3\u679c\u4e0d\u540c\uff0c\u5206\u4e3a\uff1a<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6807\u91cf\u5b50\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\u7ed3\u679c\u4e3a\u5355\u4e2a\u503c\uff09<\/li>\n\n\n\n<li>\u5217\u5b50\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\u7ed3\u679c\u4e3a\u4e00\u5217\uff09<\/li>\n\n\n\n<li>\u884c\u5b50\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\u7ed3\u679c\u4e3a\u4e00\u884c\uff09<\/li>\n\n\n\n<li>\u8868\u5b50\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\u7ed3\u679c\u4e3a\u591a\u884c\u591a\u5217\uff09<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6839\u636e\u5b50\u67e5\u8be2\u4f4d\u7f6e\uff0c\u5206\u4e3a<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>WHERE \u4e4b\u540e<\/li>\n\n\n\n<li>FROM \u4e4b\u540e<\/li>\n\n\n\n<li>SELECT \u4e4b\u540e<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u6807\u91cf\u5b50\u67e5\u8be2<\/h1>\n\n\n\n<p>\u5b50\u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u5355\u4e2a\u503c\uff08\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u65e5\u671f\u7b49\uff09\u3002<br>\u5e38\u7528\u64cd\u4f5c\u7b26\uff1a- &lt; &gt; &gt; &gt;= &lt; &lt;=<br>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--\u67e5\u8be2\u9500\u552e\u90e8\u6240\u6709\u5458\u5de5\u4fe1\u606f\n--1.\u67e5\u8be2\u9500\u552e\u90e8\u90e8\u95e8id\nselect id from dept where name='\u9500\u552e\u90e8';\n--2.\u6839\u636e\u5f97\u5230\u7684\u9500\u552e\u90e8\u90e8\u95e8id\uff0c\u67e5\u8be2\u5458\u5de5\u4fe1\u606f\uff1a\nselect * from emp where dept_id=...;\n--\u5b50\u67e5\u8be2\u5199\u6cd5\uff1a\nselect * from emp where dept_id=(select id from dept where name='\u9500\u552e\u90e8');<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001\u5217\u5b50\u67e5\u8be2<\/h1>\n\n\n\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u5217\uff08\u53ef\u4ee5\u662f\u591a\u884c\uff09\u3002<br>\u5e38\u7528\u64cd\u4f5c\u7b26\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>IN<\/td><td>\u5728\u6307\u5b9a\u7684\u96c6\u5408\u8303\u56f4\u5185\uff0c\u591a\u9009\u4e00<\/td><\/tr><tr><td>NOT IN<\/td><td>\u4e0d\u5728\u6307\u5b9a\u7684\u96c6\u5408\u8303\u56f4\u5185<\/td><\/tr><tr><td>ANY<\/td><td>\u5b50\u67e5\u8be2\u8fd4\u56de\u5217\u8868\u5185\uff0c\u6709\u4efb\u610f\u4e00\u4e2a\u6ee1\u8db3\u5373\u53ef<\/td><\/tr><tr><td>SOME<\/td><td>\u7b49\u540cANY<\/td><\/tr><tr><td>ALL<\/td><td>\u5b50\u67e5\u8be2\u5217\u8868\u7684\u6240\u6709\u5236\u90fd\u5fc5\u987b\u6ee1\u8db3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f8b\u4e00\uff1a\u67e5\u8be2\u5e02\u573a\u90e8\u548c\u9500\u552e\u90e8\u6240\u6709\u5458\u5de5\u4fe1\u606f\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>select * from emp where dept_id in (select id from dept where name='\u9500\u552e\u90e8'||name ='\u5e02\u573a\u90e8');<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f8b\u4e8c\uff1a&#8211; \u67e5\u8be2\u6bd4\u8d22\u52a1\u90e8\u6240\u6709\u4eba\u5de5\u8d44\u90fd\u9ad8\u7684\u5458\u5de5\u4fe1\u606f<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>select * from emp where salary &gt; all(select salary from emp where dept_id=(selcet id from dept where name ='\u8d22\u52a1\u90e8'));<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001\u884c\u5b50\u67e5\u8be2<\/h1>\n\n\n\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u884c\uff08\u53ef\u4ee5\u662f\u591a\u5217\uff09\u3002<br>\u5e38\u7528\u64cd\u4f5c\u7b26\uff1a=, &lt;, &gt;, IN, NOT IN<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f8b\uff1a\u67e5\u8be2\u4e0exxx\u7684\u85aa\u8d44\u53ca\u6240\u5728\u90e8\u95e8\u76f8\u540c\u7684\u5458\u5de5\u4fe1\u606f<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>select * from emp where(salary,dept_id)=(select salary,dept_id from emp where name ='XXX')\uff1b<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u56db\u3001\u8868\u5b50\u67e5\u8be2<\/h1>\n\n\n\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u591a\u884c\u591a\u5217<br>\u5e38\u7528\u64cd\u4f5c\u7b26\uff1aIN<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f8b1\uff1a\u67e5\u8be2\u4e0eAAA\uff0cBBB\u7684\u90e8\u95e8\u548c\u85aa\u8d44\u76f8\u540c\u7684\u5458\u5de5<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>select * \nfrom emp\nwhere(dept_id,salary) in (select dept_id,salary from emp where name ='AAA' or name='BBB');<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f8b2\uff1a \u67e5\u8be2\u5165\u804c\u65e5\u671f\u662f2006-01-01\u4e4b\u540e\u7684\u5458\u5de5\uff0c\u53ca\u5176\u90e8\u95e8\u4fe1\u606f<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>select e.*,d.* \nfrom(select * from emp where entrydate&gt;'2006-01-01') as e \nleft join dept as d on e.dept_id=d.id;<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u8bed\u53e5\u4e0d\u7528\u5b50\u67e5\u8be2\u4e5f\u53ef\u4ee5\u5199\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select emp.*,dept.*\nfrom emp \nleft join dept \non emp.dept_id=dept.id\nwhere entrydate&gt;'2006-01-01';<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6b22\u8fce\u8865\u5145\u4e0e\u6307\u6b63<\/p>\n","protected":false},"excerpt":{"rendered":"<p># \u5b50\u67e5\u8be2\u4ecb\u7ecd \u4f8b\u5982\uff1a \u5b50\u67e5\u8be2\u5916\u90e8\u7684\u8bed\u53e5\u53ef\u4ee5\u662f INSERT \/ UPDATE \/ DELETE \/ SELE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14,15,19],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-sql","tag-sql","tag-15","tag-19"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":1,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":117,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/117"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}