{"id":2622,"date":"2025-03-11T23:35:48","date_gmt":"2025-03-11T15:35:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2622"},"modified":"2025-03-11T23:35:48","modified_gmt":"2025-03-11T15:35:48","slug":"postgresql-and-or-%e8%bf%90%e7%ae%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/11\/postgresql-and-or-%e8%bf%90%e7%ae%97%e7%ac%a6\/","title":{"rendered":"PostgreSQL AND &#038; OR \u8fd0\u7b97\u7b26"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 SQL \u67e5\u8be2\u4e2d\uff0c<code>AND<\/code> \u548c <code>OR<\/code> \u8fd0\u7b97\u7b26\u7528\u4e8e\u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\u3002\u5b83\u4eec\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u5e38\u7528\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u591a\u6761\u4ef6\u8fc7\u6ee4\u3002<code>AND<\/code> \u8868\u793a\u6761\u4ef6\u5fc5\u987b\u5168\u90e8\u6ee1\u8db3\uff0c<code>OR<\/code> \u8868\u793a\u6761\u4ef6\u53ea\u9700\u6ee1\u8db3\u5176\u4e2d\u4e4b\u4e00\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. AND \u8fd0\u7b97\u7b26<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>AND<\/code> \u8fd0\u7b97\u7b26\u7528\u4e8e\u8fde\u63a5\u591a\u4e2a\u6761\u4ef6\uff0c\u53ea\u6709\u5f53\u6240\u6709\u6761\u4ef6\u90fd\u4e3a <code>TRUE<\/code> \u65f6\uff0c\u7ed3\u679c\u624d\u4e3a <code>TRUE<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>AND \u8fd0\u7b97\u7b26\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ...\nFROM table_name\nWHERE condition1 AND condition2 AND ...;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>AND \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e00\u4e2a <code>employees<\/code> \u8868\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(50),\n    age INT,\n    department VARCHAR(50),\n    salary DECIMAL(10,2)\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5411\u8868\u4e2d\u63d2\u5165\u4e00\u4e9b\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO employees (name, age, department, salary) VALUES\n('Alice', 30, 'HR', 6000),\n('Bob', 35, 'IT', 8000),\n('Charlie', 28, 'Finance', 5500),\n('David', 40, 'IT', 9500),\n('Eve', 25, 'HR', 5000);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2<\/strong>\uff1a\u67e5\u627e\u6240\u6709 <code>IT<\/code> \u90e8\u95e8\u5e76\u4e14\u85aa\u6c34\u5927\u4e8e 8000 \u7684\u5458\u5de5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employees WHERE department = 'IT' AND salary &gt; 8000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id | name  | age | department | salary\n---+-------+-----+-----------+--------\n4  | David |  40 | IT        | 9500.00<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u67e5\u8be2\u4e2d\uff0c\u53ea\u6709 <code>David<\/code> \u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a<code>department = 'IT'<\/code> \u548c <code>salary &gt; 8000<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. OR \u8fd0\u7b97\u7b26<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>OR<\/code> \u8fd0\u7b97\u7b26\u7528\u4e8e\u8fde\u63a5\u591a\u4e2a\u6761\u4ef6\uff0c\u53ea\u6709\u5f53\u5176\u4e2d\u81f3\u5c11\u4e00\u4e2a\u6761\u4ef6\u4e3a <code>TRUE<\/code> \u65f6\uff0c\u7ed3\u679c\u624d\u4e3a <code>TRUE<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>OR \u8fd0\u7b97\u7b26\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ...\nFROM table_name\nWHERE condition1 OR condition2 OR ...;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>OR \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2<\/strong>\uff1a\u67e5\u627e\u5e74\u9f84\u5927\u4e8e 30 \u5c81\u6216\u8005\u90e8\u95e8\u662f <code>HR<\/code> \u7684\u5458\u5de5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employees WHERE age &gt; 30 OR department = 'HR';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id | name    | age | department | salary\n---+---------+-----+-----------+--------\n1  | Alice   |  30 | HR        | 6000.00\n2  | Bob     |  35 | IT        | 8000.00\n4  | David   |  40 | IT        | 9500.00\n5  | Eve     |  25 | HR        | 5000.00<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u67e5\u8be2\u4e2d\uff0c\u6ee1\u8db3\u6761\u4ef6\u7684\u5458\u5de5\u662f\uff1a\u5e74\u9f84\u5927\u4e8e 30 \u5c81\u7684\u5458\u5de5\u548c\u90e8\u95e8\u4e3a <code>HR<\/code> \u7684\u5458\u5de5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. AND \u548c OR \u7ed3\u5408\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>AND<\/code> \u548c <code>OR<\/code> \u53ef\u4ee5\u4e00\u8d77\u4f7f\u7528\u6765\u7ec4\u5408\u66f4\u590d\u6742\u7684\u67e5\u8be2\u6761\u4ef6\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>AND<\/code> \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e <code>OR<\/code>\uff0c\u56e0\u6b64\u901a\u5e38\u9700\u8981\u4f7f\u7528\u62ec\u53f7\u6765\u660e\u786e\u6761\u4ef6\u7684\u7ec4\u5408\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>AND \u548c OR \u7ed3\u5408\u7684\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ...\nFROM table_name\nWHERE (condition1 AND condition2) OR (condition3 AND condition4);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>AND \u548c OR \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2<\/strong>\uff1a\u67e5\u627e\u5e74\u9f84\u5927\u4e8e 30 \u5c81\u4e14\u85aa\u6c34\u5927\u4e8e 8000\uff0c\u6216\u8005\u90e8\u95e8\u4e3a <code>HR<\/code> \u7684\u5458\u5de5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employees \nWHERE (age &gt; 30 AND salary &gt; 8000) OR department = 'HR';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id | name    | age | department | salary\n---+---------+-----+-----------+--------\n1  | Alice   |  30 | HR        | 6000.00\n2  | Bob     |  35 | IT        | 8000.00\n4  | David   |  40 | IT        | 9500.00<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u67e5\u8be2\u4e2d\uff0c\u7b26\u5408\u4ee5\u4e0b\u4e24\u79cd\u6761\u4ef6\u7684\u5458\u5de5\u4f1a\u88ab\u8fd4\u56de\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5e74\u9f84\u5927\u4e8e 30 \u4e14\u85aa\u6c34\u5927\u4e8e 8000\uff08\u5982 <code>Bob<\/code> \u548c <code>David<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u90e8\u95e8\u662f <code>HR<\/code>\uff08\u5982 <code>Alice<\/code>\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u4f18\u5148\u7ea7\u548c\u62ec\u53f7<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>AND<\/code> \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e <code>OR<\/code>\u3002<\/li>\n\n\n\n<li>\u4e3a\u4e86\u786e\u4fdd\u6309\u7167\u671f\u671b\u7684\u987a\u5e8f\u8bc4\u4f30\u6761\u4ef6\uff0c\u5efa\u8bae\u4f7f\u7528\u62ec\u53f7\u6765\u660e\u786e\u4f18\u5148\u7ea7\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f8b\u5b50<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employees \nWHERE age &gt; 30 OR department = 'HR' AND salary &gt; 6000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u67e5\u8be2\u4e2d\uff0c\u7531\u4e8e <code>AND<\/code> \u4f18\u5148\u7ea7\u9ad8\uff0c\u5b9e\u9645\u6548\u679c\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employees \nWHERE age &gt; 30 OR (department = 'HR' AND salary &gt; 6000);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>AND<\/code><\/strong>\uff1a\u53ea\u6709\u5f53\u6240\u6709\u6761\u4ef6\u90fd\u4e3a <code>TRUE<\/code> \u65f6\uff0c\u7ed3\u679c\u624d\u4e3a <code>TRUE<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>OR<\/code><\/strong>\uff1a\u53ea\u8981\u4efb\u610f\u4e00\u4e2a\u6761\u4ef6\u4e3a <code>TRUE<\/code>\uff0c\u7ed3\u679c\u5c31\u4e3a <code>TRUE<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u5408\u4f7f\u7528<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u62ec\u53f7\u6765\u660e\u786e\u8fd0\u7b97\u7684\u4f18\u5148\u7ea7\uff0c\u786e\u4fdd\u67e5\u8be2\u903b\u8f91\u7684\u51c6\u786e\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e24\u79cd\u8fd0\u7b97\u7b26\u662f\u6784\u5efa\u590d\u6742\u67e5\u8be2\u65f6\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5b83\u4eec\uff0c\u4f60\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u8fc7\u6ee4\u6570\u636e\uff01\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQL \u67e5\u8be2\u4e2d\uff0cAND \u548c OR \u8fd0\u7b97\u7b26\u7528\u4e8e\u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\u3002\u5b83\u4eec\u5728 WHERE \u5b50\u53e5\u4e2d\u5e38\u7528\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u591a\u6761\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2623,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2622","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2622"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2622\/revisions"}],"predecessor-version":[{"id":2624,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2622\/revisions\/2624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2623"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}