{"id":213,"date":"2024-06-17T20:49:16","date_gmt":"2024-06-17T12:49:16","guid":{"rendered":"http:\/\/zhaoyanqi.cn\/?p=213"},"modified":"2024-06-17T20:49:17","modified_gmt":"2024-06-17T12:49:17","slug":"sql%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/zhaoyanqi.cn\/?p=213","title":{"rendered":"SQL\u591a\u8868\u67e5\u8be2"},"content":{"rendered":"\n<p id=\"u334a7127\">\u5355\u8868\u67e5\u8be2\u5185\u5bb9\u53c2\u8003\uff1a<a href=\"https:\/\/www.yuque.com\/u39220520\/rtqkia\/rbmtd38g1hg1logf\">https:\/\/www.yuque.com\/u39220520\/rtqkia\/rbmtd38g1hg1logf<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Y2b4R\">\u94fe\u8868\u67e5\u8be2<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Er8BJ\">\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h3>\n\n\n\n<p id=\"u90b0b4b8\"><strong>\u5efa\u8868<\/strong><\/p>\n\n\n\n<pre id=\"53af2650\" class=\"wp-block-code\"><code>CREATE TABLE department (\n    id INT,\n    name VARCHAR(20)\n);\n\nCREATE TABLE employee (\n    id INT PRIMARY KEY AUTO_INCREMENT,\n    name VARCHAR(20),\n    sex ENUM('male', 'female') NOT NULL DEFAULT 'male',\n    age INT,\n    dep_id INT\n);<\/code><\/pre>\n\n\n\n<p id=\"u194ec3f0\"><strong>\u63d2\u5165\u6570\u636e<\/strong><\/p>\n\n\n\n<pre id=\"0489e526\" class=\"wp-block-code\"><code>INSERT INTO department VALUES\n(200, '\u6280\u672f'),\n(201, '\u4eba\u529b\u8d44\u6e90'),\n(202, '\u9500\u552e'),\n(203, '\u8fd0\u8425');\n\nINSERT INTO employee (name, sex, age, dep_id) VALUES\n('egon', 'male', 18, 200),\n('alex', 'female', 48, 201),\n('wupeiqi', 'male', 38, 201),\n('yuanhao', 'female', 28, 202),\n('liwenzhou', 'male', 18, 200),\n('jingliyang', 'female', 18, 204);<\/code><\/pre>\n\n\n\n<p id=\"ud17693ad\"><strong>\u67e5\u770b\u8868\u7ed3\u6784\u548c\u6570\u636e<\/strong><\/p>\n\n\n\n<pre id=\"3245f3cf\" class=\"wp-block-code\"><code>DESC department;<\/code><\/pre>\n\n\n\n<p id=\"u333276ef\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"edaa0402\" class=\"wp-block-code\"><code>+-------+-------------+------+-----+---------+-------+\n| Field | Type        | Null | Key | Default | Extra |\n+-------+-------------+------+-----+---------+-------+\n| id    | int(11)     | YES  |     | NULL    |       |\n| name  | varchar(20) | YES  |     | NULL    |       |\n+-------+-------------+------+-----+---------+-------+<\/code><\/pre>\n\n\n\n<pre id=\"82e66ac0\" class=\"wp-block-code\"><code>DESC employee;<\/code><\/pre>\n\n\n\n<p id=\"u2564150d\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"7f20199d\" class=\"wp-block-code\"><code>+--------+-----------------------+------+-----+---------+----------------+\n| Field  | Type                  | Null | Key | Default | Extra          |\n+--------+-----------------------+------+-----+---------+----------------+\n| id     | int(11)               | NO   | PRI | NULL    | auto_increment |\n| name   | varchar(20)           | YES  |     | NULL    |                |\n| sex    | enum('male','female') | NO   |     | male    |                |\n| age    | int(11)               | YES  |     | NULL    |                |\n| dep_id | int(11)               | YES  |     | NULL    |                |\n+--------+-----------------------+------+-----+---------+----------------+<\/code><\/pre>\n\n\n\n<pre id=\"f3d0521c\" class=\"wp-block-code\"><code>SELECT * FROM department;<\/code><\/pre>\n\n\n\n<p id=\"ufe645e9d\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"905a9987\" class=\"wp-block-code\"><code>+------+--------------+\n| id   | name         |\n+------+--------------+\n| 200  | \u6280\u672f         |\n| 201  | \u4eba\u529b\u8d44\u6e90     |\n| 202  | \u9500\u552e         |\n| 203  | \u8fd0\u8425         |\n+------+--------------+<\/code><\/pre>\n\n\n\n<pre id=\"b1b100a3\" class=\"wp-block-code\"><code>SELECT * FROM employee;<\/code><\/pre>\n\n\n\n<p id=\"u30f8a1f1\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"e2c93344\" class=\"wp-block-code\"><code>+----+------------+--------+------+--------+\n| id | name       | sex    | age  | dep_id |\n+----+------------+--------+------+--------+\n|  1 | egon       | male   |   18 |    200 |\n|  2 | alex       | female |   48 |    201 |\n|  3 | wupeiqi    | male   |   38 |    201 |\n|  4 | yuanhao    | female |   28 |    202 |\n|  5 | liwenzhou  | male   |   18 |    200 |\n|  6 | jingliyang | female |   18 |    204 |\n+----+------------+--------+------+--------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"HYss8\">\u4e8c\u3001\u591a\u8868\u8fde\u63a5\u67e5\u8be2<\/h3>\n\n\n\n<p id=\"uaaa59e02\">\u591a\u8868\u8fde\u63a5\u67e5\u8be2\u7528\u4e8e\u4ece\u591a\u4e2a\u8868\u4e2d\u83b7\u53d6\u76f8\u5173\u6570\u636e\u3002\u5e38\u89c1\u7684\u591a\u8868\u8fde\u63a5\u5305\u62ec\u4ea4\u53c9\u8fde\u63a5\u3001\u5185\u8fde\u63a5\u3001\u5de6\u8fde\u63a5\u3001\u53f3\u8fde\u63a5\u548c\u5168\u5916\u8fde\u63a5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"dHNOs\">1. \u4ea4\u53c9\u8fde\u63a5<\/h4>\n\n\n\n<p id=\"u846ed0cf\">\u4ea4\u53c9\u8fde\u63a5\u4e0d\u4f7f\u7528\u4efb\u4f55\u5339\u914d\u6761\u4ef6\uff0c\u751f\u6210\u7b1b\u5361\u5c14\u79ef\uff1a<\/p>\n\n\n\n<pre id=\"8468b718\" class=\"wp-block-code\"><code>SELECT * FROM employee, department;<\/code><\/pre>\n\n\n\n<p id=\"ubbc60d4c\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"f3b00cdc\" class=\"wp-block-code\"><code>+----+------------+--------+------+--------+------+--------------+\n| id | name       | sex    | age  | dep_id | id   | name         |\n+----+------------+--------+------+--------+------+--------------+\n|  1 | egon       | male   |   18 |    200 |  200 | \u6280\u672f         |\n|  1 | egon       | male   |   18 |    200 |  201 | \u4eba\u529b\u8d44\u6e90     |\n|  1 | egon       | male   |   18 |    200 |  202 | \u9500\u552e         |\n|  1 | egon       | male   |   18 |    200 |  203 | \u8fd0\u8425         |\n|  2 | alex       | female |   48 |    201 |  200 | \u6280\u672f         |\n|  2 | alex       | female |   48 |    201 |  201 | \u4eba\u529b\u8d44\u6e90     |\n|  2 | alex       | female |   48 |    201 |  202 | \u9500\u552e         |\n|  2 | alex       | female |   48 |    201 |  203 | \u8fd0\u8425         |\n|  3 | wupeiqi    | male   |   38 |    201 |  200 | \u6280\u672f         |\n|  3 | wupeiqi    | male   |   38 |    201 |  201 | \u4eba\u529b\u8d44\u6e90     |\n|  3 | wupeiqi    | male   |   38 |    201 |  202 | \u9500\u552e         |\n|  3 | wupeiqi    | male   |   38 |    201 |  203 | \u8fd0\u8425         |\n|  4 | yuanhao    | female |   28 |    202 |  200 | \u6280\u672f         |\n|  4 | yuanhao    | female |   28 |    202 |  201 | \u4eba\u529b\u8d44\u6e90     |\n|  4 | yuanhao    | female |   28 |    202 |  202 | \u9500\u552e         |\n|  4 | yuanhao    | female |   28 |    202 |  203 | \u8fd0\u8425         |\n|  5 | liwenzhou  | male   |   18 |    200 |  200 | \u6280\u672f         |\n|  5 | liwenzhou  | male   |   18 |    200 |  201 | \u4eba\u529b\u8d44\u6e90     |\n|  5 | liwenzhou  | male   |   18 |    200 |  202 | \u9500\u552e         |\n|  5 | liwenzhou  | male   |   18 |    200 |  203 | \u8fd0\u8425         |\n|  6 | jingliyang | female |   18 |    204 |  200 | \u6280\u672f         |\n|  6 | jingliyang | female |   18 |    204 |  201 | \u4eba\u529b\u8d44\u6e90     |\n|  6 | jingliyang | female |   18 |    204 |  202 | \u9500\u552e         |\n|  6 | jingliyang | female |   18 |    204 |  203 | \u8fd0\u8425         |\n+----+------------+--------+------+--------+------+--------------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"OB788\">2. \u5185\u8fde\u63a5<\/h4>\n\n\n\n<p id=\"ud01382cd\">\u5185\u8fde\u63a5\u53ea\u8fde\u63a5\u5339\u914d\u7684\u884c\uff1a<\/p>\n\n\n\n<pre id=\"8ca1f137\" class=\"wp-block-code\"><code>SELECT employee.id, employee.name, employee.age, employee.sex, department.name \nFROM employee \nINNER JOIN department ON employee.dep_id = department.id;<\/code><\/pre>\n\n\n\n<p id=\"uf7807c7d\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"449a3f05\" class=\"wp-block-code\"><code>+----+-----------+------+--------+--------------+\n| id | name      | age  | sex    | name         |\n+----+-----------+------+--------+--------------+\n|  1 | egon      |   18 | male   | \u6280\u672f         |\n|  2 | alex      |   48 | female | \u4eba\u529b\u8d44\u6e90     |\n|  3 | wupeiqi   |   38 | male   | \u4eba\u529b\u8d44\u6e90     |\n|  4 | yuanhao   |   28 | female | \u9500\u552e         |\n|  5 | liwenzhou |   18 | male   | \u6280\u672f         |\n+----+-----------+------+--------+--------------+<\/code><\/pre>\n\n\n\n<p id=\"u16215657\">\u4e0a\u8ff0 SQL \u7b49\u540c\u4e8e\uff1a<\/p>\n\n\n\n<pre id=\"86fc828d\" class=\"wp-block-code\"><code>SELECT employee.id, employee.name, employee.age, employee.sex, department.name \nFROM employee, department \nWHERE employee.dep_id = department.id;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"rSIPL\">3. \u5de6\u8fde\u63a5\uff08\u5de6\u5916\u8fde\u63a5\uff09<\/h4>\n\n\n\n<p id=\"uaa24f9dd\">\u5de6\u8fde\u63a5\u4f18\u5148\u663e\u793a\u5de6\u8868\u5168\u90e8\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre id=\"5c1f04ac\" class=\"wp-block-code\"><code>SELECT employee.id, employee.name, department.name AS depart_name \nFROM employee \nLEFT JOIN department ON employee.dep_id = department.id;<\/code><\/pre>\n\n\n\n<p id=\"u4869e559\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"ee002745\" class=\"wp-block-code\"><code>+----+------------+--------------+\n| id | name       | depart_name  |\n+----+------------+--------------+\n|  1 | egon       | \u6280\u672f         |\n|  5 | liwenzhou  | \u6280\u672f         |\n|  2 | alex       | \u4eba\u529b\u8d44\u6e90     |\n|  3 | wupeiqi    | \u4eba\u529b\u8d44\u6e90     |\n|  4 | yuanhao    | \u9500\u552e         |\n|  6 | jingliyang | NULL         |\n+----+------------+--------------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"o6aVD\">4. \u53f3\u8fde\u63a5\uff08\u53f3\u5916\u8fde\u63a5\uff09<\/h4>\n\n\n\n<p id=\"uc51228d4\">\u53f3\u8fde\u63a5\u4f18\u5148\u663e\u793a\u53f3\u8868\u5168\u90e8\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre id=\"67e64756\" class=\"wp-block-code\"><code>SELECT employee.id, employee.name, department.name AS\n\n depart_name \nFROM employee \nRIGHT JOIN department ON employee.dep_id = department.id;<\/code><\/pre>\n\n\n\n<p id=\"u7b87bc5a\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"af3a680b\" class=\"wp-block-code\"><code>+------+-----------+--------------+\n| id   | name      | depart_name  |\n+------+-----------+--------------+\n|    1 | egon      | \u6280\u672f         |\n|    2 | alex      | \u4eba\u529b\u8d44\u6e90     |\n|    3 | wupeiqi   | \u4eba\u529b\u8d44\u6e90     |\n|    4 | yuanhao   | \u9500\u552e         |\n|    5 | liwenzhou | \u6280\u672f         |\n| NULL | NULL      | \u8fd0\u8425         |\n+------+-----------+--------------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"ysIJD\">5. \u5168\u5916\u8fde\u63a5<\/h4>\n\n\n\n<p id=\"ucb379b83\">\u5168\u5916\u8fde\u63a5\u663e\u793a\u5de6\u53f3\u4e24\u4e2a\u8868\u5168\u90e8\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre id=\"69ba0ff3\" class=\"wp-block-code\"><code>SELECT * \nFROM employee LEFT JOIN department ON employee.dep_id = department.id\nUNION\nSELECT * \nFROM employee RIGHT JOIN department ON employee.dep_id = department.id;<\/code><\/pre>\n\n\n\n<p id=\"u94f32d45\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"8b77c0b7\" class=\"wp-block-code\"><code>+------+------------+--------+------+--------+------+--------------+\n| id   | name       | sex    | age  | dep_id | id   | name         |\n+------+------------+--------+------+--------+------+--------------+\n|    1 | egon       | male   |   18 |    200 |  200 | \u6280\u672f         |\n|    5 | liwenzhou  | male   |   18 |    200 |  200 | \u6280\u672f         |\n|    2 | alex       | female |   48 |    201 |  201 | \u4eba\u529b\u8d44\u6e90     |\n|    3 | wupeiqi    | male   |   38 |    201 |  201 | \u4eba\u529b\u8d44\u6e90     |\n|    4 | yuanhao    | female |   28 |    202 |  202 | \u9500\u552e         |\n|    6 | jingliyang | female |   18 |    204 | NULL | NULL         |\n| NULL | NULL       | NULL   | NULL |   NULL |  203 | \u8fd0\u8425         |\n+------+------------+--------+------+--------+------+--------------+<\/code><\/pre>\n\n\n\n<p id=\"ufdbaf880\">\u6ce8\u610f\uff1a<code>UNION<\/code> \u4f1a\u53bb\u6389\u76f8\u540c\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"HNdhZ\">\u4e09\u3001\u7b26\u5408\u6761\u4ef6\u8fde\u63a5\u67e5\u8be2<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"DfhDa\">\u793a\u4f8b 1<\/h4>\n\n\n\n<p id=\"u413db5d2\">\u4ee5\u5185\u8fde\u63a5\u7684\u65b9\u5f0f\u67e5\u8be2 <code>employee<\/code> \u548c <code>department<\/code> \u8868\uff0c\u5e76\u4e14 <code>employee<\/code> \u8868\u4e2d\u7684 <code>age<\/code> \u5b57\u6bb5\u503c\u5fc5\u987b\u5927\u4e8e 25\uff0c\u5373\u627e\u51fa\u5e74\u9f84\u5927\u4e8e 25 \u5c81\u7684\u5458\u5de5\u4ee5\u53ca\u5458\u5de5\u6240\u5728\u7684\u90e8\u95e8\uff1a<\/p>\n\n\n\n<pre id=\"3b6ca929\" class=\"wp-block-code\"><code>SELECT employee.name, department.name \nFROM employee \nINNER JOIN department ON employee.dep_id = department.id \nWHERE age &gt; 25;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"m1sbV\">\u793a\u4f8b 2<\/h4>\n\n\n\n<p id=\"ued86967b\">\u4ee5\u5185\u8fde\u63a5\u7684\u65b9\u5f0f\u67e5\u8be2 <code>employee<\/code> \u548c <code>department<\/code> \u8868\uff0c\u5e76\u4e14\u4ee5 <code>age<\/code> \u5b57\u6bb5\u7684\u5347\u5e8f\u65b9\u5f0f\u663e\u793a\uff1a<\/p>\n\n\n\n<pre id=\"db9bcee4\" class=\"wp-block-code\"><code>SELECT employee.id, employee.name, employee.age, department.name \nFROM employee, department \nWHERE employee.dep_id = department.id \nAND age &gt; 25 \nORDER BY age ASC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"cda4aa3c\">\u56db\u3001\u5b50\u67e5\u8be2<\/h3>\n\n\n\n<p id=\"u6b713395\">\u5b50\u67e5\u8be2\u662f\u5c06\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u5d4c\u5957\u5728\u53e6\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u4e2d\u3002\u5185\u5c42\u67e5\u8be2\u8bed\u53e5\u7684\u67e5\u8be2\u7ed3\u679c\uff0c\u53ef\u4ee5\u4e3a\u5916\u5c42\u67e5\u8be2\u8bed\u53e5\u63d0\u4f9b\u67e5\u8be2\u6761\u4ef6\u3002\u5b50\u67e5\u8be2\u4e2d\u53ef\u4ee5\u5305\u542b\uff1a<code>IN<\/code>\u3001<code>NOT IN<\/code>\u3001<code>ANY<\/code>\u3001<code>ALL<\/code>\u3001<code>EXISTS<\/code> \u548c <code>NOT EXISTS<\/code> \u7b49\u5173\u952e\u5b57\uff0c\u8fd8\u53ef\u4ee5\u5305\u542b\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff1a<code>=<\/code> \u3001 <code>!=<\/code>\u3001<code>&gt;<\/code> \u3001<code>&lt;<\/code> \u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"94bf4211\">1. \u5e26 <code>IN<\/code> \u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<p id=\"u76eee56f\"><strong>\u67e5\u8be2\u5e73\u5747\u5e74\u9f84\u5728 25 \u5c81\u4ee5\u4e0a\u7684\u90e8\u95e8\u540d<\/strong><\/p>\n\n\n\n<pre id=\"5c79b5c6\" class=\"wp-block-code\"><code>SELECT id, name FROM department\nWHERE id IN \n    (SELECT dep_id FROM employee GROUP BY dep_id HAVING AVG(age) &gt; 25);<\/code><\/pre>\n\n\n\n<p id=\"ua435d578\"><strong>\u67e5\u770b\u6280\u672f\u90e8\u5458\u5de5\u59d3\u540d<\/strong><\/p>\n\n\n\n<pre id=\"52ae789d\" class=\"wp-block-code\"><code>SELECT name FROM employee\nWHERE dep_id IN \n    (SELECT id FROM department WHERE name='\u6280\u672f');<\/code><\/pre>\n\n\n\n<p id=\"u82ff31e5\"><strong>\u67e5\u770b\u4e0d\u8db3 1 \u4eba\u7684\u90e8\u95e8\u540d\uff08\u5b50\u67e5\u8be2\u5f97\u5230\u7684\u662f\u6709\u4eba\u7684\u90e8\u95e8 id\uff09<\/strong><\/p>\n\n\n\n<pre id=\"6ccde457\" class=\"wp-block-code\"><code>SELECT name FROM department \nWHERE id NOT IN (SELECT DISTINCT dep_id FROM employee);<\/code><\/pre>\n\n\n\n<p id=\"uaa6dcf77\"><strong>\u6ce8\u610f\uff1a<\/strong><code><strong>NOT IN<\/strong><\/code><strong> \u65e0\u6cd5\u5904\u7406 <\/strong><code><strong>NULL<\/strong><\/code><strong> \u7684\u503c<\/strong><\/p>\n\n\n\n<p id=\"u3737162e\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre id=\"516109df\" class=\"wp-block-code\"><code>SELECT * FROM employee WHERE dep_id NOT IN (SELECT DISTINCT dep_id FROM employee);<\/code><\/pre>\n\n\n\n<p id=\"u53285c37\">\u5982\u679c\u5b50\u67e5\u8be2\u4e2d\u5b58\u5728 <code>NULL<\/code>\uff0c\u9700\u8981\u52a0\u4e0a <code>IS NOT NULL<\/code> \u6761\u4ef6\uff1a<\/p>\n\n\n\n<pre id=\"fb230aa5\" class=\"wp-block-code\"><code>SELECT * FROM department \nWHERE id NOT IN (SELECT DISTINCT dep_id FROM employee WHERE dep_id IS NOT NULL);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"a5789827\">2. \u5e26 <code>ANY<\/code> \u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<p id=\"u304655df\">\u5728 SQL \u4e2d <code>ANY<\/code> \u548c <code>SOME<\/code> \u662f\u540c\u4e49\u8bcd\u3002<\/p>\n\n\n\n<p id=\"u0c33efb4\"><strong>\u793a\u4f8b<\/strong><\/p>\n\n\n\n<pre id=\"841149b7\" class=\"wp-block-code\"><code>SELECT * FROM employee \nWHERE salary = ANY (\n    SELECT MAX(salary) FROM employee GROUP BY dep_id\n);\n\nSELECT * FROM employee \nWHERE salary IN (\n    SELECT MAX(salary) FROM employee GROUP BY dep_id\n);<\/code><\/pre>\n\n\n\n<p id=\"u486d91e5\"><strong>\u6ce8\u610f\uff1a<\/strong><code><strong>ANY<\/strong><\/code><strong> \u5fc5\u987b\u548c\u5176\u4ed6\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5171\u540c\u4f7f\u7528<\/strong><\/p>\n\n\n\n<pre id=\"cc04670e\" class=\"wp-block-code\"><code>SELECT * FROM T_Book \nWHERE FYearPublished &lt; ANY (2001, 2003, 2005);  -- \u9519\u8bef\u793a\u4f8b<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"72119389\">3. \u5e26 <code>ALL<\/code> \u5173\u952e\u5b57\u7684\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<p id=\"uf30db3c3\"><code>ALL<\/code> \u8868\u793a\u6240\u6709\uff0c<code>ANY<\/code> \u8868\u793a\u4efb\u4e00\u3002<\/p>\n\n\n\n<p id=\"u98874413\"><strong>\u67e5\u8be2\u51fa\u90a3\u4e9b\u85aa\u8d44\u6bd4\u6240\u6709\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\u90fd\u9ad8\u7684\u5458\u5de5<\/strong><\/p>\n\n\n\n<pre id=\"802f51aa\" class=\"wp-block-code\"><code>SELECT * FROM employee \nWHERE salary &gt; ALL (\n    SELECT AVG(salary) FROM employee GROUP BY dep_id\n);<\/code><\/pre>\n\n\n\n<p id=\"udb1a841f\"><strong>\u67e5\u8be2\u51fa\u90a3\u4e9b\u85aa\u8d44\u6bd4\u6240\u6709\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\u90fd\u4f4e\u7684\u5458\u5de5<\/strong><\/p>\n\n\n\n<pre id=\"b14a35b0\" class=\"wp-block-code\"><code>SELECT * FROM employee \nWHERE salary &lt; ALL (\n    SELECT AVG(salary) FROM employee GROUP BY dep_id\n);<\/code><\/pre>\n\n\n\n<p id=\"u3f3dcc38\"><strong>\u67e5\u8be2\u51fa\u90a3\u4e9b\u85aa\u8d44\u6bd4\u4efb\u610f\u4e00\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\u4f4e\u7684\u5458\u5de5<\/strong><\/p>\n\n\n\n<pre id=\"3196f229\" class=\"wp-block-code\"><code>SELECT * FROM employee \nWHERE salary &lt; ANY (\n    SELECT AVG(salary) FROM employee GROUP BY dep_id\n);<\/code><\/pre>\n\n\n\n<p id=\"u73e20823\"><strong>\u67e5\u8be2\u51fa\u90a3\u4e9b\u85aa\u8d44\u6bd4\u4efb\u610f\u4e00\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\u9ad8\u7684\u5458\u5de5<\/strong><\/p>\n\n\n\n<pre id=\"36e59270\" class=\"wp-block-code\"><code>SELECT * FROM employee \nWHERE salary &gt; ANY (\n    SELECT AVG(salary) FROM employee GROUP BY dep_id\n);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"28508faf\">4. \u5e26\u6bd4\u8f83\u8fd0\u7b97\u7b26\u7684\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<p id=\"u8f1d6578\"><strong>\u67e5\u8be2\u5927\u4e8e\u6240\u6709\u4eba\u5e73\u5747\u5e74\u9f84\u7684\u5458\u5de5\u540d\u4e0e\u5e74\u9f84<\/strong><\/p>\n\n\n\n<pre id=\"9335cf63\" class=\"wp-block-code\"><code>SELECT name, age FROM employee \nWHERE age &gt; (SELECT AVG(age) FROM employee);<\/code><\/pre>\n\n\n\n<p id=\"u261455c5\"><strong>\u67e5\u8be2\u5927\u4e8e\u90e8\u95e8\u5185\u5e73\u5747\u5e74\u9f84\u7684\u5458\u5de5\u540d\u3001\u5e74\u9f84<\/strong><\/p>\n\n\n\n<pre id=\"fda0c11a\" class=\"wp-block-code\"><code>SELECT t1.name, t1.age FROM employee t1\nINNER JOIN (\n    SELECT dep_id, AVG(age) AS avg_age FROM employee GROUP BY dep_id\n) t2 ON t1.dep_id = t2.dep_id\nWHERE t1.age &gt; t2.avg_age;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"25f9c7fa\">\u67e5\u8be2\u7ec3\u4e60\uff1a\u67e5\u8be2\u6bcf\u4e2a\u90e8\u95e8\u6700\u65b0\u5165\u804c\u7684\u90a3\u4f4d\u5458\u5de5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"bc4e05e5\">\u8868\u7ed3\u6784\u548c\u6570\u636e\u51c6\u5907<\/h4>\n\n\n\n<p id=\"udde0aa2a\">\u5047\u8bbe\u6709\u5982\u4e0b\u7684 <code>company.employee<\/code> \u8868\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\u5217\u540d<\/td><td>\u5b57\u6bb5\u540d<\/td><td>\u7c7b\u578b<\/td><\/tr><tr><td>\u5458\u5de5id<\/td><td>id<\/td><td>int<\/td><\/tr><tr><td>\u59d3\u540d<\/td><td>emp_name<\/td><td>varchar<\/td><\/tr><tr><td>\u6027\u522b<\/td><td>sex<\/td><td>enum<\/td><\/tr><tr><td>\u5e74\u9f84<\/td><td>age<\/td><td>int<\/td><\/tr><tr><td>\u5165\u804c\u65e5\u671f<\/td><td>hire_date<\/td><td>date<\/td><\/tr><tr><td>\u5c97\u4f4d<\/td><td>post<\/td><td>varchar<\/td><\/tr><tr><td>\u804c\u4f4d\u63cf\u8ff0<\/td><td>post_comment<\/td><td>varchar<\/td><\/tr><tr><td>\u85aa\u6c34<\/td><td>salary<\/td><td>double<\/td><\/tr><tr><td>\u529e\u516c\u5ba4<\/td><td>office<\/td><td>int<\/td><\/tr><tr><td>\u90e8\u95e8\u7f16\u53f7<\/td><td>depart_id<\/td><td>int<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"b9cdb1ae\">\u521b\u5efa\u8868<\/h4>\n\n\n\n<pre id=\"5b7e5cc2\" class=\"wp-block-code\"><code>CREATE TABLE employee (\n    id INT NOT NULL UNIQUE AUTO_INCREMENT,\n    name VARCHAR(20) NOT NULL,\n    sex ENUM('male', 'female') NOT NULL DEFAULT 'male',\n    age INT(3) UNSIGNED NOT NULL DEFAULT 28,\n    hire_date DATE NOT NULL,\n    post VARCHAR(50),\n    post_comment VARCHAR(100),\n    salary DOUBLE(15,2),\n    office INT,\n    depart_id INT\n);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"ea44402b\">\u63d2\u5165\u8bb0\u5f55<\/h4>\n\n\n\n<pre id=\"7d930077\" class=\"wp-block-code\"><code>INSERT INTO employee (name, sex, age, hire_date, post, salary, office, depart_id) VALUES\n('egon', 'male', 18, '2017-03-01', '\u8001\u7537\u5b69\u9a7b\u6c99\u6cb3\u529e\u4e8b\u5904\u5916\u4ea4\u5927\u4f7f', 7300.33, 401, 1), -- \u6559\u5b66\u90e8\n('alex', 'male', 78, '2015-03-02', 'teacher', 1000000.31, 401, 1),\n('wupeiqi', 'male', 81, '2013-03-05', 'teacher', 8300, 401, 1),\n('yuanhao', 'male', 73, '2014-07-01', 'teacher', 3500, 401, 1),\n('liwenzhou', 'male', 28, '2012-11-01', 'teacher', 2100, 401, 1),\n('jingliyang', 'female', 18, '2011-02-11', 'teacher', 9000, 401, 1),\n('jinxin', 'male', 18, '1900-03-01', 'teacher', 30000, 401, 1),\n('\u6210\u9f99', 'male', 48, '2010-11-11', 'teacher', 10000, 401, 1),\n\n('\u6b6a\u6b6a', 'female', 48, '2015-03-11', 'sale', 3000.13, 402, 2), -- \u9500\u552e\u90e8\u95e8\n('\u4e2b\u4e2b', 'female', 38, '2010-11-01', 'sale', 2000.35, 402, 2),\n('\u4e01\u4e01', 'female', 18, '2011-03-12', 'sale', 1000.37, 402, 2),\n('\u661f\u661f', 'female', 18, '2016-05-13', 'sale', 3000.29, 402, 2),\n('\u683c\u683c', 'female', 28, '2017-01-27', 'sale', 4000.33, 402, 2),\n\n('\u5f20\u91ce', 'male', 28, '2016-03-11', 'operation', 10000.13, 403, 3), -- \u8fd0\u8425\u90e8\u95e8\n('\u7a0b\u54ac\u91d1', 'male', 18, '1997-03-12', 'operation', 20000, 403, 3),\n('\u7a0b\u54ac\u94f6', 'female', 18, '2013-03-11', 'operation', 19000, 403, 3),\n('\u7a0b\u54ac\u94dc', 'male', 18, '2015-04-11', 'operation', 18000, 403, 3),\n('\u7a0b\u54ac\u94c1', 'female', 18, '2014-05-12', 'operation', 17000, 403, 3);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"b13411d7\">\u67e5\u770b\u8868\u7ed3\u6784<\/h4>\n\n\n\n<pre id=\"tzj5i\" class=\"wp-block-code\"><code>DESC employee;<\/code><\/pre>\n\n\n\n<p id=\"u99de17f0\">\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"187eabb9\" class=\"wp-block-code\"><code>+--------------+-----------------------+------+-----+---------+----------------+\n| Field        | Type                  | Null | Key | Default | Extra          |\n+--------------+-----------------------+------+-----+---------+----------------+\n| id           | int(11)               | NO   | PRI | NULL    | auto_increment |\n| name         | varchar(20)           | NO   |     | NULL    |                |\n| sex          | enum('male','female') | NO   |     | male    |                |\n| age          | int(3) unsigned       | NO   |     | 28      |                |\n| hire_date    | date                  | NO   |     | NULL    |                |\n| post         | varchar(50)           | YES  |     | NULL    |                |\n| post_comment | varchar(100)          | YES  |     | NULL    |                |\n| salary       | double(15,2)          | YES  |     | NULL    |                |\n| office       | int(11)               | YES  |     | NULL    |                |\n| depart_id    | int(11)               | YES  |     | NULL    |                |\n+--------------+-----------------------+------+-----+---------+----------------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"a6120619\">\u67e5\u8be2\u6bcf\u4e2a\u90e8\u95e8\u6700\u65b0\u5165\u804c\u7684\u90a3\u4f4d\u5458\u5de5<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"meisi\"><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 <\/strong><code><strong>INNER JOIN<\/strong><\/code><\/h5>\n\n\n\n<pre id=\"46a4e61c\" class=\"wp-block-code\"><code>SELECT t1.*\nFROM employee AS t1\nINNER JOIN (\n    SELECT post, MAX(hire_date) AS max_date\n    FROM employee\n    GROUP BY post\n) AS t2 ON t1.post = t2.post\nWHERE t1.hire_date = t2.max_date;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"WCeRc\"><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528\u5b50\u67e5\u8be2\u548c <\/strong><code><strong>LIMIT<\/strong><\/code><\/h5>\n\n\n\n<pre id=\"6b4d5d3a\" class=\"wp-block-code\"><code>SELECT t3.name, t3.post, t3.hire_date\nFROM employee AS t3\nWHERE id IN (\n    SELECT (\n        SELECT id FROM employee AS t2\n        WHERE t2.post = t1.post\n        ORDER BY hire_date DESC\n        LIMIT 1\n    ) FROM employee AS t1\n    GROUP BY post\n);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u8868\u67e5\u8be2\u5185\u5bb9\u53c2\u8003\uff1ahttps:\/\/www.yuque.com\/u39220520\/rtqkia\/rbmtd38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,12],"tags":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-mysql","category-sql"],"_links":{"self":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213"}],"version-history":[{"count":1,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions\/214"}],"wp:attachment":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}