{"id":209,"date":"2024-06-17T12:05:19","date_gmt":"2024-06-17T04:05:19","guid":{"rendered":"http:\/\/zhaoyanqi.cn\/?p=209"},"modified":"2024-06-17T12:05:21","modified_gmt":"2024-06-17T04:05:21","slug":"sql%e5%8d%95%e8%a1%a8%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/zhaoyanqi.cn\/?p=209","title":{"rendered":"SQL\u5355\u8868\u67e5\u8be2"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"UMyHy\">\u4e00\u3001\u5355\u8868\u67e5\u8be2\u7684\u8bed\u6cd5<\/h2>\n\n\n\n<pre id=\"c4fee76d\" class=\"wp-block-code\"><code>SELECT \u5b57\u6bb51, \u5b57\u6bb52, ... \nFROM \u8868\u540d\nWHERE \u6761\u4ef6\nGROUP BY \u5b57\u6bb5\nHAVING \u7b5b\u9009\nORDER BY \u5b57\u6bb5\nLIMIT \u9650\u5236\u6761\u6570;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"tEBYw\">\u4e8c\u3001\u5173\u952e\u5b57\u7684\u6267\u884c\u4f18\u5148\u7ea7\uff08\u91cd\u8981\uff09<\/h2>\n\n\n\n<p id=\"u3164ff5d\"><strong>\u6267\u884c\u987a\u5e8f\uff08\u4f18\u5148\u7ea7\uff09<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>FROM<\/code><\/li>\n\n\n\n<li><code>WHERE<\/code><\/li>\n\n\n\n<li><code>GROUP BY<\/code><\/li>\n\n\n\n<li><code>HAVING<\/code><\/li>\n\n\n\n<li><code>SELECT<\/code><\/li>\n\n\n\n<li><code>DISTINCT<\/code><\/li>\n\n\n\n<li><code>ORDER BY<\/code><\/li>\n\n\n\n<li><code>LIMIT<\/code><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ea2ea769\">\u7ec6\u89e3\u91ca<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u627e\u5230\u8868<\/strong> (<code>FROM<\/code>)\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ece\u6307\u5b9a\u7684\u8868\u4e2d\u5f00\u59cb\u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<pre id=\"459b547d\" class=\"wp-block-code\"><code>SELECT \u5b57\u6bb51, \u5b57\u6bb52 \nFROM \u8868\u540d;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>\u62ff\u7740 <\/strong><code><strong>WHERE<\/strong><\/code><strong> \u6307\u5b9a\u7684\u7ea6\u675f\u6761\u4ef6<\/strong>\uff0c\u53bb\u8868\u4e2d\u53d6\u51fa\u4e00\u6761\u6761\u8bb0\u5f55\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6839\u636e <code>WHERE<\/code> \u5b50\u53e5\u7684\u6761\u4ef6\u7b5b\u9009\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<pre id=\"bcc44bac\" class=\"wp-block-code\"><code>SELECT \u5b57\u6bb51, \u5b57\u6bb52 \nFROM \u8868\u540d \nWHERE \u6761\u4ef6;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>\u5c06\u53d6\u51fa\u7684\u4e00\u6761\u6761\u8bb0\u5f55\u8fdb\u884c\u5206\u7ec4<\/strong> (<code>GROUP BY<\/code>)\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309\u7167\u6307\u5b9a\u7684\u5b57\u6bb5\u5bf9\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\u3002<\/li>\n<\/ul>\n\n\n\n<pre id=\"7c5e4e82\" class=\"wp-block-code\"><code>SELECT \u5b57\u6bb51, COUNT(\u5b57\u6bb52) \nFROM \u8868\u540d \nWHERE \u6761\u4ef6 \nGROUP BY \u5b57\u6bb51;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>\u5c06\u5206\u7ec4\u7684\u7ed3\u679c\u8fdb\u884c <\/strong><code><strong>HAVING<\/strong><\/code><strong> \u8fc7\u6ee4<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7b5b\u9009\u3002<\/li>\n<\/ul>\n\n\n\n<pre id=\"47485f80\" class=\"wp-block-code\"><code>SELECT \u5b57\u6bb51, COUNT(\u5b57\u6bb52) \nFROM \u8868\u540d \nWHERE \u6761\u4ef6 \nGROUP BY \u5b57\u6bb51 \nHAVING COUNT(\u5b57\u6bb52) &gt; 1;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li><strong>\u6267\u884c <\/strong><code><strong>SELECT<\/strong><\/code>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9009\u62e9\u9700\u8981\u663e\u793a\u7684\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<pre id=\"47485f80-1\" class=\"wp-block-code\"><code>SELECT \u5b57\u6bb51, COUNT(\u5b57\u6bb52) \nFROM \u8868\u540d \nWHERE \u6761\u4ef6 \nGROUP BY \u5b57\u6bb51 \nHAVING COUNT(\u5b57\u6bb52) &gt; 1;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li><strong>\u53bb\u91cd<\/strong> (<code>DISTINCT<\/code>)\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ece\u7ed3\u679c\u96c6\u4e2d\u53bb\u9664\u91cd\u590d\u7684\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<pre id=\"1898871e\" class=\"wp-block-code\"><code>SELECT DISTINCT \u5b57\u6bb51 \nFROM \u8868\u540d \nWHERE \u6761\u4ef6;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"7\">\n<li><strong>\u5c06\u7ed3\u679c\u6309\u6761\u4ef6\u6392\u5e8f<\/strong> (<code>ORDER BY<\/code>)\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309\u7167\u6307\u5b9a\u7684\u5b57\u6bb5\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<pre id=\"6c737722\" class=\"wp-block-code\"><code>SELECT \u5b57\u6bb51, \u5b57\u6bb52 \nFROM \u8868\u540d \nWHERE \u6761\u4ef6 \nORDER BY \u5b57\u6bb51 DESC;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"8\">\n<li><strong>\u9650\u5236\u7ed3\u679c\u7684\u663e\u793a\u6761\u6570<\/strong> (<code>LIMIT<\/code>)\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9650\u5236\u8fd4\u56de\u7684\u7ed3\u679c\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<pre id=\"fafafd06\" class=\"wp-block-code\"><code>SELECT \u5b57\u6bb51, \u5b57\u6bb52 \nFROM \u8868\u540d \nWHERE \u6761\u4ef6 \nORDER BY \u5b57\u6bb51 \nLIMIT 10;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"92f447a1\">\u5173\u952e\u5b57\u6267\u884c\u4f18\u5148\u7ea7\u7684\u5b8c\u6574\u793a\u4f8b<\/h3>\n\n\n\n<p id=\"u7c3f1b7a\">\u5047\u8bbe\u6709\u4e00\u4e2a <code>students<\/code> \u8868\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>id<\/td><td>name<\/td><td>age<\/td><td>email<\/td><td>born_year<\/td><td>reg_time<\/td><\/tr><tr><td>1<\/td><td>egon1<\/td><td>18<\/td><td><a href=\"mailto:123123@qq.com\" target=\"_blank\" rel=\"noreferrer noopener\">123123@qq.com<\/a><\/td><td>1999<\/td><td>2023-06-12 10:00:00<\/td><\/tr><tr><td>2<\/td><td>egon2<\/td><td>28<\/td><td><a href=\"mailto:123123@163.com\" target=\"_blank\" rel=\"noreferrer noopener\">123123@163.com<\/a><\/td><td>1999<\/td><td>2023-06-12 10:00:00<\/td><\/tr><tr><td>3<\/td><td>egon3<\/td><td>38<\/td><td><a href=\"mailto:123123@xx.com\" target=\"_blank\" rel=\"noreferrer noopener\">123123@xx.com<\/a><\/td><td>1999<\/td><td>2023-06-12 10:00:00<\/td><\/tr><tr><td>4<\/td><td>egon4<\/td><td>48<\/td><td><a href=\"mailto:123123@aa.com\" target=\"_blank\" rel=\"noreferrer noopener\">123123@aa.com<\/a><\/td><td>1999<\/td><td>2023-06-12 10:00:00<\/td><\/tr><tr><td>5<\/td><td>egon5<\/td><td>58<\/td><td><a href=\"mailto:123123@zz.com\" target=\"_blank\" rel=\"noreferrer noopener\">123123@zz.com<\/a><\/td><td>1999<\/td><td>2023-06-12 10:00:00<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p id=\"u843bb1b9\">\u6211\u4eec\u8981\u6267\u884c\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n\n\n\n<pre id=\"7da864c2\" class=\"wp-block-code\"><code>SELECT name, COUNT(*) \nFROM students \nWHERE age &gt; 20 \nGROUP BY name \nHAVING COUNT(*) &gt; 1 \nORDER BY name \nLIMIT 3;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"cb6c41bf\">\u5173\u952e\u5b57\u6267\u884c\u987a\u5e8f\u89e3\u91ca<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>FROM<\/strong>\uff1a\u627e\u5230\u8868 <code>students<\/code>\u3002<\/li>\n\n\n\n<li><strong>WHERE<\/strong>\uff1a\u7b5b\u9009\u51fa <code>age > 20<\/code> \u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><strong>GROUP BY<\/strong>\uff1a\u6309 <code>name<\/code> \u8fdb\u884c\u5206\u7ec4\u3002<\/li>\n\n\n\n<li><strong>HAVING<\/strong>\uff1a\u5bf9\u5206\u7ec4\u7ed3\u679c\u8fdb\u884c\u7b5b\u9009\uff0c\u4fdd\u7559 <code>COUNT(*) > 1<\/code> \u7684\u7ec4\u3002<\/li>\n\n\n\n<li><strong>SELECT<\/strong>\uff1a\u9009\u62e9 <code>name<\/code> \u548c <code>COUNT(*)<\/code> \u4f5c\u4e3a\u7ed3\u679c\u5b57\u6bb5\u3002<\/li>\n\n\n\n<li><strong>DISTINCT<\/strong>\uff1a\u53bb\u9664\u91cd\u590d\u7684\u884c\uff08\u6b64\u67e5\u8be2\u672a\u4f7f\u7528 <code>DISTINCT<\/code>\uff0c\u8df3\u8fc7\uff09\u3002<\/li>\n\n\n\n<li><strong>ORDER BY<\/strong>\uff1a\u6309 <code>name<\/code> \u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n\n\n\n<li><strong>LIMIT<\/strong>\uff1a\u9650\u5236\u8fd4\u56de\u7684\u7ed3\u679c\u6570\u91cf\u4e3a 3 \u6761\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"CGvFx\">\u4e09\u3001\u7b80\u5355\u67e5\u8be2<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"vWCCM\">\u51c6\u5907<\/h3>\n\n\n\n<p id=\"u8ea81eec\">\u5047\u8bbe\u6709\u4e00\u4e2a <code>company.employee<\/code> \u8868\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\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=\"dfdf32d2\" 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',  -- \u5927\u90e8\u5206\u662f\u7537\u7684\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,  -- \u4e00\u4e2a\u90e8\u95e8\u4e00\u4e2a\u5c4b\u5b50\n    depart_id INT\n);<\/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=\"82e66ac0\" class=\"wp-block-code\"><code>DESC employee;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"ea44402b\">\u63d2\u5165\u8bb0\u5f55<\/h4>\n\n\n\n<pre id=\"ffe92f5e\" 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('\u6b6a\u6b6a', 'female', 48, '2015-03-11', 'sale', 3000.13, 402, 2),  -- \u9500\u552e\u90e8\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('\u5f20\u91ce', 'male', 28, '2016-03-11', 'operation', 10000.13, 403, 3),  -- \u8fd0\u8425\u90e8\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=\"81e282d4\">\u8bbe\u7f6e\u5b57\u7b26\u7f16\u7801\u4e3a GBK\uff08\u9002\u7528\u4e8e Windows \u7cfb\u7edf\uff09<\/h4>\n\n\n\n<pre id=\"97ff6b56\" class=\"wp-block-code\"><code>ALTER TABLE employee CONVERT TO CHARACTER SET gbk;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"o3VML\">\u7b80\u5355\u67e5\u8be2\u8bed\u53e5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"9f380f34\">\u67e5\u8be2\u6240\u6709\u5b57\u6bb5<\/h4>\n\n\n\n<pre id=\"8b0668c1\" class=\"wp-block-code\"><code>SELECT id, name, sex, age, hire_date, post, post_comment, salary, office, depart_id \nFROM employee;<\/code><\/pre>\n\n\n\n<p id=\"u043ba6e8\">\u6216<\/p>\n\n\n\n<pre id=\"b1b100a3\" class=\"wp-block-code\"><code>SELECT * FROM employee;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"48e3b289\">\u67e5\u8be2\u7279\u5b9a\u5b57\u6bb5<\/h4>\n\n\n\n<pre id=\"9e41ddb4\" class=\"wp-block-code\"><code>SELECT name, salary FROM employee;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"8a8b3ffd\">\u907f\u514d\u91cd\u590d (DISTINCT)<\/h4>\n\n\n\n<pre id=\"49944903\" class=\"wp-block-code\"><code>SELECT DISTINCT post FROM employee;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"76863095\">\u901a\u8fc7\u56db\u5219\u8fd0\u7b97\u67e5\u8be2<\/h4>\n\n\n\n<pre id=\"4ea31b5d\" class=\"wp-block-code\"><code>SELECT name, salary * 12 FROM employee;<\/code><\/pre>\n\n\n\n<p id=\"u47220eb3\">\u6216<\/p>\n\n\n\n<pre id=\"b01100d3\" class=\"wp-block-code\"><code>SELECT name, salary * 12 AS annual_salary FROM employee;<\/code><\/pre>\n\n\n\n<p id=\"u4e9686ad\">\u6216<\/p>\n\n\n\n<pre id=\"415e9925\" class=\"wp-block-code\"><code>SELECT name, salary * 12 annual_salary FROM employee;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"359fd570\">\u5b9a\u4e49\u663e\u793a\u683c\u5f0f\uff08\u53ef\u7528\u4e8e\u6570\u636e\u8fc1\u79fb\uff0c\u6570\u636e\u5bfc\u51fa\u5bfc\u5165\uff09<\/h4>\n\n\n\n<p id=\"u2c01afb1\"><strong>\u4f7f\u7528 <\/strong><code><strong>CONCAT()<\/strong><\/code><strong> \u51fd\u6570<\/strong><\/p>\n\n\n\n<pre id=\"a22837c1\" class=\"wp-block-code\"><code>SELECT CONCAT('\u59d3\u540d: ', name, '  \u5e74\u85aa: ', salary * 12) AS annual_salary \nFROM employee;<\/code><\/pre>\n\n\n\n<p id=\"u8b8d4c47\"><strong>\u4f7f\u7528 <\/strong><code><strong>CONCAT_WS()<\/strong><\/code><strong> \u51fd\u6570<\/strong><\/p>\n\n\n\n<pre id=\"702caba3\" class=\"wp-block-code\"><code>SELECT CONCAT_WS(':', name, salary * 12) AS annual_salary \nFROM employee;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"d8d4df49\">\u7ed3\u5408 <code>CASE<\/code> \u8bed\u53e5<\/h4>\n\n\n\n<pre id=\"1fd26da3\" class=\"wp-block-code\"><code>SELECT\n    (\n        CASE\n            WHEN name = 'egon' THEN name\n            WHEN name = 'alex' THEN CONCAT(name, '_BIGSB')\n            ELSE CONCAT(name, 'SB')\n        END\n    ) AS new_name\nFROM employee;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"xOOwv\">\u56db\u3001WHERE \u7ea6\u675f<\/h2>\n\n\n\n<p id=\"ua820c605\">\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u8fd0\u7b97\u7b26\u548c\u5173\u952e\u5b57\u6765\u8fdb\u884c\u6761\u4ef6\u67e5\u8be2\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/strong>\uff1a<code>><\/code>, <code>&lt;<\/code>, <code>>=<\/code>, <code>&lt;=<\/code>, <code>&lt;><\/code>, <code>!=<\/code><\/li>\n\n\n\n<li><strong>\u8303\u56f4\u8fd0\u7b97\u7b26<\/strong>\uff1a<code>BETWEEN ... AND ...<\/code> \u503c\u5728\u6307\u5b9a\u8303\u56f4\u5185<\/li>\n\n\n\n<li><strong>\u96c6\u5408\u8fd0\u7b97\u7b26<\/strong>\uff1a<code>IN (...)<\/code> \u503c\u5728\u6307\u5b9a\u96c6\u5408\u4e2d<\/li>\n\n\n\n<li><strong>\u6a21\u5f0f\u5339\u914d<\/strong>\uff1a<code>LIKE 'pattern'<\/code>\uff0c\u5176\u4e2d <code>pattern<\/code> \u53ef\u4ee5\u4f7f\u7528 <code>%<\/code> \u8868\u793a\u4efb\u610f\u591a\u5b57\u7b26\uff0c<code>_<\/code> \u8868\u793a\u4e00\u4e2a\u5b57\u7b26<\/li>\n\n\n\n<li><strong>\u903b\u8f91\u8fd0\u7b97\u7b26<\/strong>\uff1a<code>AND<\/code>, <code>OR<\/code>, <code>NOT<\/code><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"kNNbL\">\u793a\u4f8b\u67e5\u8be2<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"XN7WS\"><strong>1. \u5355\u6761\u4ef6\u67e5\u8be2<\/strong><\/h4>\n\n\n\n<pre id=\"3670258f\" class=\"wp-block-code\"><code>SELECT name FROM employee\nWHERE post = 'sale';<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"WLJgK\"><strong>2. \u591a\u6761\u4ef6\u67e5\u8be2<\/strong><\/h4>\n\n\n\n<pre id=\"c6965a8e\" class=\"wp-block-code\"><code>SELECT name, salary FROM employee\nWHERE post = 'teacher' AND salary &gt; 10000;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"LQWko\"><strong>3. \u5173\u952e\u5b57 <\/strong><code><strong>BETWEEN ... AND ...<\/strong><\/code><\/h4>\n\n\n\n<pre id=\"401c68a6\" class=\"wp-block-code\"><code>SELECT name, salary FROM employee \nWHERE salary BETWEEN 10000 AND 20000;\n\nSELECT name, salary FROM employee \nWHERE salary NOT BETWEEN 10000 AND 20000;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"eDaMy\"><strong>4. \u5173\u952e\u5b57 <\/strong><code><strong>IS NULL<\/strong><\/code><\/h4>\n\n\n\n<pre id=\"89b157fb\" class=\"wp-block-code\"><code>SELECT name, post_comment FROM employee \nWHERE post_comment IS NULL;\n\nSELECT name, post_comment FROM employee \nWHERE post_comment IS NOT NULL;\n\nSELECT name, post_comment FROM employee \nWHERE post_comment = '';  -- \u6ce8\u610f\uff1a'' \u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u4e0d\u662f NULL<\/code><\/pre>\n\n\n\n<pre id=\"b909d738\" class=\"wp-block-code\"><code>-- \u5c06\u67d0\u4e00\u884c\u7684 post_comment \u5b57\u6bb5\u8bbe\u4e3a\u7a7a\u5b57\u7b26\u4e32\nUPDATE employee SET post_comment = '' WHERE id = 2;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"c37sA\"><strong>5. \u5173\u952e\u5b57 <\/strong><code><strong>IN<\/strong><\/code><strong> \u96c6\u5408\u67e5\u8be2<\/strong><\/h4>\n\n\n\n<pre id=\"a03f9541\" class=\"wp-block-code\"><code>SELECT name, salary FROM employee \nWHERE salary = 3000 OR salary = 3500 OR salary = 4000 OR salary = 9000;\n\nSELECT name, salary FROM employee \nWHERE salary IN (3000, 3500, 4000, 9000);\n\nSELECT name, salary FROM employee \nWHERE salary NOT IN (3000, 3500, 4000, 9000);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Hn4AS\"><strong>6. \u5173\u952e\u5b57 <\/strong><code><strong>LIKE<\/strong><\/code><strong> \u6a21\u7cca\u67e5\u8be2<\/strong><\/h4>\n\n\n\n<pre id=\"1a32ff64\" class=\"wp-block-code\"><code>-- \u901a\u914d\u7b26 `%`\nSELECT * FROM employee \nWHERE name LIKE 'eg%';\n\n-- \u901a\u914d\u7b26 `_`\nSELECT * FROM employee \nWHERE name LIKE 'al__';<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d6f1L\">\u4e94\u3001\u5206\u7ec4\u67e5\u8be2: <code>GROUP BY<\/code><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"wT1II\">\u4e00\u3001\u4ec0\u4e48\u662f\u5206\u7ec4\uff1f\u4e3a\u4ec0\u4e48\u8981\u5206\u7ec4\uff1f<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5206\u7ec4\u53d1\u751f\u5728 <\/strong><code><strong>WHERE<\/strong><\/code><strong> \u4e4b\u540e<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5206\u7ec4\u662f\u57fa\u4e8e <code>WHERE<\/code> \u4e4b\u540e\u5f97\u5230\u7684\u8bb0\u5f55\u8fdb\u884c\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>\u5206\u7ec4\u7684\u5b9a\u4e49<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5206\u7ec4\u662f\u5c06\u6240\u6709\u8bb0\u5f55\u6309\u7167\u67d0\u4e2a\u76f8\u540c\u5b57\u6bb5\u8fdb\u884c\u5f52\u7c7b\uff0c\u6bd4\u5982\u6839\u636e\u5458\u5de5\u4fe1\u606f\u8868\u7684\u804c\u4f4d\u5206\u7ec4\uff0c\u6216\u6309\u6027\u522b\u5206\u7ec4\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>\u5206\u7ec4\u7684\u76ee\u7684<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u83b7\u53d6\u6bcf\u4e2a\u90e8\u95e8\u7684\u6700\u9ad8\u5de5\u8d44<\/li>\n\n\n\n<li>\u83b7\u53d6\u6bcf\u4e2a\u90e8\u95e8\u7684\u5458\u5de5\u6570<\/li>\n\n\n\n<li>\u83b7\u53d6\u7537\u6027\u548c\u5973\u6027\u7684\u6570\u91cf<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>\u5206\u7ec4\u4f9d\u636e<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u6309\u7167\u4efb\u610f\u5b57\u6bb5\u5206\u7ec4\uff0c\u4f46\u5206\u7ec4\u5b8c\u6bd5\u540e\uff0c\u6bd4\u5982 <code>GROUP BY post<\/code>\uff0c\u53ea\u80fd\u67e5\u770b <code>post<\/code> \u5b57\u6bb5\u3002\u5982\u679c\u60f3\u67e5\u770b\u7ec4\u5185\u4fe1\u606f\uff0c\u9700\u8981\u501f\u52a9\u805a\u5408\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"u7pIe\">\u4e8c\u3001ONLY_FULL_GROUP_BY \u6a21\u5f0f<\/h3>\n\n\n\n<p id=\"u83f3ec34\"><strong>\u67e5\u770b MySQL 5.7 \u9ed8\u8ba4\u7684 <\/strong><code><strong>sql_mode<\/strong><\/code>\uff1a<\/p>\n\n\n\n<pre id=\"cc7e2698\" class=\"wp-block-code\"><code>SELECT @@global.sql_mode;<\/code><\/pre>\n\n\n\n<p id=\"u118e8d17\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"903cfc1b\" class=\"wp-block-code\"><code>ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION<\/code><\/pre>\n\n\n\n<p id=\"uddcf79f4\"><strong>ONLY_FULL_GROUP_BY \u6a21\u5f0f\u8bf4\u660e<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <code>ONLY_FULL_GROUP_BY<\/code> \u6a21\u5f0f\u4e0b\uff0c<code>SELECT<\/code> \u76ee\u6807\u5217\u8868\u4e2d\u7684\u6240\u6709\u5217\u7684\u503c\u8981\u4e48\u662f\u805a\u5408\u51fd\u6570\u7684\u7ed3\u679c\uff0c\u8981\u4e48\u662f <code>GROUP BY<\/code> \u5217\u8868\u4e2d\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p id=\"u52270602\"><strong>\u8bbe\u7f6e <\/strong><code><strong>sql_mode<\/strong><\/code><strong>\uff08\u53bb\u6389 <\/strong><code><strong>ONLY_FULL_GROUP_BY<\/strong><\/code><strong> \u6a21\u5f0f\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre id=\"39e0d249\" class=\"wp-block-code\"><code>SET GLOBAL sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';<\/code><\/pre>\n\n\n\n<p id=\"uecdefde1\">\u91cd\u65b0\u67e5\u770b <code>sql_mode<\/code>\uff1a<\/p>\n\n\n\n<pre id=\"cc7e2698-1\" class=\"wp-block-code\"><code>SELECT @@global.sql_mode;<\/code><\/pre>\n\n\n\n<p id=\"u0959220a\">\u8f93\u51fa\u7ed3\u679c\u4e3a\u7a7a\uff1a<\/p>\n\n\n\n<pre id=\"319aa2dd\" class=\"wp-block-code\"><code>+-------------------+\n| @@global.sql_mode |\n+-------------------+\n|                   |\n+-------------------+\n1 row in set (0.00 sec)<\/code><\/pre>\n\n\n\n<p id=\"u8d7f6360\"><strong>\u5728\u6ca1\u6709 <\/strong><code><strong>ONLY_FULL_GROUP_BY<\/strong><\/code><strong> \u6a21\u5f0f\u4e0b\u7684\u67e5\u8be2<\/strong>\uff1a<\/p>\n\n\n\n<pre id=\"e24119e3\" class=\"wp-block-code\"><code>SELECT * FROM emp GROUP BY post;<\/code><\/pre>\n\n\n\n<p id=\"u3cca287e\">\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre id=\"a2952070\" class=\"wp-block-code\"><code>+----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+\n| id | name | sex    | age | hire_date  | post                       | post_comment | salary     | office | depart_id |\n+----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+\n| 14 | \u5f20\u91ce | male   |  28 | 2016-03-11 | operation                  | NULL         |   10000.13 |    403 |         3 |\n|  9 | \u6b6a\u6b6a | female |  48 | 2015-03-11 | sale                       | NULL         |    3000.13 |    402 |         2 |\n|  2 | alex | male   |  78 | 2015-03-02 | teacher                    | NULL         | 1000000.31 |    401 |         1 |\n|  1 | egon | male   |  18 | 2017-03-01 | \u8001\u7537\u5b69\u9a7b\u6c99\u6cb3\u529e\u4e8b\u5904\u5916\u4ea4\u5927\u4f7f | NULL         |    7300.33 |    401 |         1 |\n+----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+\n4 rows in set (0.00 sec)<\/code><\/pre>\n\n\n\n<p id=\"u180b9c47\"><strong>\u542f\u7528 <\/strong><code><strong>ONLY_FULL_GROUP_BY<\/strong><\/code><strong> \u6a21\u5f0f<\/strong>\uff1a<\/p>\n\n\n\n<pre id=\"5870eef1\" class=\"wp-block-code\"><code>SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY';<\/code><\/pre>\n\n\n\n<p id=\"ucb830516\">\u91cd\u542f MySQL \u4f7f\u8bbe\u7f6e\u751f\u6548\uff1a<\/p>\n\n\n\n<pre id=\"1dcd3f5f\" class=\"wp-block-code\"><code>mysql&gt; quit<\/code><\/pre>\n\n\n\n<p id=\"u28852159\">\u91cd\u65b0\u767b\u5f55\u5e76\u4f7f\u7528\u6570\u636e\u5e93\uff1a<\/p>\n\n\n\n<pre id=\"e517de4e\" class=\"wp-block-code\"><code>USE db1;<\/code><\/pre>\n\n\n\n<p id=\"uf21a6024\"><strong>\u5728\u542f\u7528 <\/strong><code><strong>ONLY_FULL_GROUP_BY<\/strong><\/code><strong> \u6a21\u5f0f\u4e0b\u7684\u67e5\u8be2<\/strong>\uff1a<\/p>\n\n\n\n<pre id=\"e24119e3-1\" class=\"wp-block-code\"><code>SELECT * FROM emp GROUP BY post;<\/code><\/pre>\n\n\n\n<p id=\"u4e8b6c14\">\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre id=\"95f0db47\" class=\"wp-block-code\"><code>ERROR 1055 (42000): 'db1.emp.id' isn't in GROUP BY<\/code><\/pre>\n\n\n\n<p id=\"udc1d6ed7\">\u6b63\u786e\u67e5\u8be2\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre id=\"f694df98\" class=\"wp-block-code\"><code>SELECT post, COUNT(id) FROM emp GROUP BY post;<\/code><\/pre>\n\n\n\n<p id=\"u5e833002\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"df5a94bb\" class=\"wp-block-code\"><code>+----------------------------+-----------+\n| post                       | count(id) |\n+----------------------------+-----------+\n| operation                  |         5 |\n| sale                       |         5 |\n| teacher                    |         7 |\n| \u8001\u7537\u5b69\u9a7b\u6c99\u6cb3\u529e\u4e8b\u5904\u5916\u4ea4\u5927\u4f7f |         1 |\n+----------------------------+-----------+\n4 rows in set (0.00 sec)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"BCHmK\">\u4e09\u3001<code>GROUP BY<\/code> \u67e5\u8be2<\/h3>\n\n\n\n<p id=\"ub4a0feb8\"><strong>\u5355\u72ec\u4f7f\u7528 <\/strong><code><strong>GROUP BY<\/strong><\/code><strong> \u5173\u952e\u5b57\u5206\u7ec4<\/strong>\uff1a<\/p>\n\n\n\n<pre id=\"3241bea1\" class=\"wp-block-code\"><code>SELECT post FROM employee GROUP BY post;<\/code><\/pre>\n\n\n\n<p id=\"u1a2a7233\">\u6ce8\u610f\uff1a\u6309\u7167 <code>post<\/code> \u5b57\u6bb5\u5206\u7ec4\uff0c<code>SELECT<\/code> \u67e5\u8be2\u7684\u5b57\u6bb5\u53ea\u80fd\u662f <code>post<\/code>\uff0c\u60f3\u8981\u83b7\u53d6\u7ec4\u5185\u7684\u5176\u4ed6\u76f8\u5173\u4fe1\u606f\uff0c\u9700\u8981\u501f\u52a9\u805a\u5408\u51fd\u6570\u3002<\/p>\n\n\n\n<p id=\"u8776f7da\"><code><strong>GROUP BY<\/strong><\/code><strong> \u5173\u952e\u5b57\u548c <\/strong><code><strong>GROUP_CONCAT()<\/strong><\/code><strong> \u51fd\u6570\u4e00\u8d77\u4f7f\u7528<\/strong>\uff1a<\/p>\n\n\n\n<pre id=\"56219d99\" class=\"wp-block-code\"><code>SELECT post, GROUP_CONCAT(name) FROM employee GROUP BY post;<\/code><\/pre>\n\n\n\n<p id=\"ud2a3a3da\">\u6309\u5c97\u4f4d\u5206\u7ec4\uff0c\u5e76\u67e5\u770b\u7ec4\u5185\u6210\u5458\u540d\uff1a<\/p>\n\n\n\n<pre id=\"9d00222e\" class=\"wp-block-code\"><code>SELECT post, GROUP_CONCAT(name) AS emp_members FROM employee GROUP BY post;<\/code><\/pre>\n\n\n\n<p id=\"ufd31749e\"><code><strong>GROUP BY<\/strong><\/code><strong> \u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528<\/strong>\uff1a<\/p>\n\n\n\n<pre id=\"ab7cfc9a\" class=\"wp-block-code\"><code>SELECT post, COUNT(id) AS count FROM employee GROUP BY post;<\/code><\/pre>\n\n\n\n<p id=\"uecfaf9c8\">\u6309\u5c97\u4f4d\u5206\u7ec4\uff0c\u5e76\u67e5\u770b\u6bcf\u4e2a\u7ec4\u6709\u591a\u5c11\u4eba\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"wvLcQ\">\u56db\u3001\u805a\u5408\u51fd\u6570<\/h3>\n\n\n\n<p id=\"u2f77345c\"><strong>\u5f3a\u8c03<\/strong>\uff1a\u805a\u5408\u51fd\u6570\u805a\u5408\u7684\u662f\u7ec4\u7684\u5185\u5bb9\uff0c\u82e5\u662f\u6ca1\u6709\u5206\u7ec4\uff0c\u5219\u9ed8\u8ba4\u4e00\u7ec4\u3002<\/p>\n\n\n\n<p id=\"u491e73ac\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre id=\"FqE5P\" class=\"wp-block-code\"><code>SELECT COUNT(*) FROM employee;<\/code><\/pre>\n\n\n\n<pre id=\"80cf0b4f\" class=\"wp-block-code\"><code>SELECT COUNT(*) FROM employee WHERE depart_id = 1;<\/code><\/pre>\n\n\n\n<pre id=\"186aa2e3\" class=\"wp-block-code\"><code>SELECT MAX(salary) FROM employee;<\/code><\/pre>\n\n\n\n<pre id=\"880c0e6e\" class=\"wp-block-code\"><code>SELECT MIN(salary) FROM employee;<\/code><\/pre>\n\n\n\n<pre id=\"d453c238\" class=\"wp-block-code\"><code>SELECT AVG(salary) FROM employee;<\/code><\/pre>\n\n\n\n<pre id=\"J4k0i\" class=\"wp-block-code\"><code>SELECT SUM(salary) FROM employee;<\/code><\/pre>\n\n\n\n<pre id=\"2e047eef\" class=\"wp-block-code\"><code>SELECT SUM(salary) FROM employee WHERE depart_id = 3;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"juW5J\">\u516d\u3001HAVING \u8fc7\u6ee4<\/h2>\n\n\n\n<p id=\"u0729dfc8\"><strong>HAVING<\/strong> \u4e0e <strong>WHERE<\/strong> \u7684\u4e0d\u540c\u4e4b\u5904\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6267\u884c\u4f18\u5148\u7ea7\u4ece\u9ad8\u5230\u4f4e<\/strong>\uff1a<code>WHERE<\/code> > <code>GROUP BY<\/code> > <code>HAVING<\/code><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>WHERE<\/strong> \u53d1\u751f\u5728\u5206\u7ec4 (<code>GROUP BY<\/code>) \u4e4b\u524d\uff0c\u56e0\u6b64 <strong>WHERE<\/strong> \u4e2d\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u5b57\u6bb5\uff0c\u4f46<strong>\u4e0d\u80fd\u4f7f\u7528\u805a\u5408\u51fd\u6570<\/strong>\u3002<\/li>\n\n\n\n<li><strong>HAVING<\/strong> \u53d1\u751f\u5728\u5206\u7ec4 (<code>GROUP BY<\/code>) \u4e4b\u540e\uff0c\u56e0\u6b64 <strong>HAVING<\/strong> \u4e2d<strong>\u53ef\u4ee5\u4f7f\u7528\u5206\u7ec4\u7684\u5b57\u6bb5\u548c\u805a\u5408\u51fd\u6570<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"gaY8S\">\u793a\u4f8b<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"NNv6B\">\u9519\u8bef\u793a\u8303<\/h4>\n\n\n\n<p id=\"u417e3163\"><strong>\u67e5\u770b MySQL \u7684 <\/strong><code><strong>sql_mode<\/strong><\/code><strong> \u8bbe\u7f6e<\/strong><\/p>\n\n\n\n<pre id=\"e017dbc5\" class=\"wp-block-code\"><code>SELECT @@sql_mode;<\/code><\/pre>\n\n\n\n<p id=\"u27b017cf\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"204b14af\" class=\"wp-block-code\"><code>+--------------------+\n| @@sql_mode         |\n+--------------------+\n| ONLY_FULL_GROUP_BY |\n+--------------------+\n1 row in set (0.00 sec)<\/code><\/pre>\n\n\n\n<p id=\"u5d75d081\"><strong>\u4f7f\u7528 <\/strong><code><strong>WHERE<\/strong><\/code><strong> \u8fdb\u884c\u8fc7\u6ee4<\/strong><\/p>\n\n\n\n<pre id=\"e5121bae\" class=\"wp-block-code\"><code>SELECT * FROM emp WHERE salary &gt; 100000;<\/code><\/pre>\n\n\n\n<p id=\"ub83666b8\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"3ecc2edf\" class=\"wp-block-code\"><code>+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+\n| id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |\n+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+\n|  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |\n+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+\n1 row in set (0.00 sec)<\/code><\/pre>\n\n\n\n<p id=\"u04929989\"><strong>\u4f7f\u7528 <\/strong><code><strong>HAVING<\/strong><\/code><strong> \u8fdb\u884c\u8fc7\u6ee4<\/strong><\/p>\n\n\n\n<pre id=\"5d71c2e9\" class=\"wp-block-code\"><code>SELECT * FROM emp HAVING salary &gt; 100000;<\/code><\/pre>\n\n\n\n<p id=\"u6185f722\">\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre id=\"5e2a0087\" class=\"wp-block-code\"><code>ERROR 1463 (42000): Non-grouping field 'salary' is used in HAVING clause<\/code><\/pre>\n\n\n\n<p id=\"u0f9b54e7\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"pY51n\"><strong>\u6b63\u786e\u4f7f\u7528\u65b9\u5f0f<\/strong><\/h4>\n\n\n\n<p id=\"ufce454cc\"><strong>\u4f7f\u7528\u5206\u7ec4\u548c\u805a\u5408\u51fd\u6570\u4e0e <\/strong><code><strong>HAVING<\/strong><\/code><\/p>\n\n\n\n<pre id=\"7f44e042\" class=\"wp-block-code\"><code>SELECT post, GROUP_CONCAT(name) \nFROM emp \nGROUP BY post \nHAVING AVG(salary) &gt; 10000;<\/code><\/pre>\n\n\n\n<p id=\"uf233967c\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"eac70909\" class=\"wp-block-code\"><code>+-----------+-------------------------------------------------------+\n| post      | group_concat(name)                                    |\n+-----------+-------------------------------------------------------+\n| operation | \u7a0b\u54ac\u94c1,\u7a0b\u54ac\u94dc,\u7a0b\u54ac\u94f6,\u7a0b\u54ac\u91d1,\u5f20\u91ce                     |\n| teacher   | \u6210\u9f99,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex |\n+-----------+-------------------------------------------------------+\n2 rows in set (0.00 sec)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"kfQSQ\">\u5c0f\u7ec3\u4e60<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"09c56939\">1. \u67e5\u8be2\u5404\u5c97\u4f4d\u5185\u5305\u542b\u7684\u5458\u5de5\u4e2a\u6570\u5c0f\u4e8e2\u7684\u5c97\u4f4d\u540d\u3001\u5c97\u4f4d\u5185\u5305\u542b\u5458\u5de5\u540d\u5b57\u3001\u4e2a\u6570<\/h4>\n\n\n\n<pre id=\"afc3dfc6\" class=\"wp-block-code\"><code>SELECT post, GROUP_CONCAT(name) AS emp_names, COUNT(id) AS emp_count \nFROM employee \nGROUP BY post \nHAVING COUNT(id) &lt; 2;<\/code><\/pre>\n\n\n\n<p id=\"u6d9dc068\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"857f52bb\" class=\"wp-block-code\"><code>+-----------------------------------------+--------------------+-----------+\n| post                                    | emp_names          | emp_count |\n+-----------------------------------------+--------------------+-----------+\n| \u8001\u7537\u5b69\u9a7b\u6c99\u6cb3\u529e\u4e8b\u5904\u5916\u4ea4\u5927\u4f7f              | egon               |         1 |\n+-----------------------------------------+--------------------+-----------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"d6309435\">2. \u67e5\u8be2\u5404\u5c97\u4f4d\u5e73\u5747\u85aa\u8d44\u5927\u4e8e10000\u7684\u5c97\u4f4d\u540d\u3001\u5e73\u5747\u5de5\u8d44<\/h4>\n\n\n\n<pre id=\"1ae80929\" class=\"wp-block-code\"><code>SELECT post, AVG(salary) AS avg_salary \nFROM employee \nGROUP BY post \nHAVING AVG(salary) &gt; 10000;<\/code><\/pre>\n\n\n\n<p id=\"u58715398\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"c1031dbc\" class=\"wp-block-code\"><code>+-----------+---------------+\n| post      | avg_salary    |\n+-----------+---------------+\n| operation | 16800.026000  |\n| teacher   | 151842.901429 |\n+-----------+---------------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"df3219d6\">3. \u67e5\u8be2\u5404\u5c97\u4f4d\u5e73\u5747\u85aa\u8d44\u5927\u4e8e10000\u4e14\u5c0f\u4e8e20000\u7684\u5c97\u4f4d\u540d\u3001\u5e73\u5747\u5de5\u8d44<\/h4>\n\n\n\n<pre id=\"b1a5c375\" class=\"wp-block-code\"><code>SELECT post, AVG(salary) AS avg_salary \nFROM employee \nGROUP BY post \nHAVING AVG(salary) &gt; 10000 AND AVG(salary) &lt; 20000;<\/code><\/pre>\n\n\n\n<p id=\"uee7f3a98\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre id=\"e6890fd4\" class=\"wp-block-code\"><code>+-----------+--------------+\n| post      | avg_salary   |\n+-----------+--------------+\n| operation | 16800.026000 |\n+-----------+--------------+<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"aTEd1\">\u4e03\u3001\u67e5\u8be2\u6392\u5e8f\uff1a<code>ORDER BY<\/code><\/h2>\n\n\n\n<p id=\"u5344d190\">\u6392\u5e8f\u67e5\u8be2\u662f\u6307\u6839\u636e\u67d0\u4e2a\u5b57\u6bb5\u7684\u503c\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u6309\u5355\u5217\u6216\u591a\u5217\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"cPIN2\">\u6309\u5355\u5217\u6392\u5e8f<\/h3>\n\n\n\n<pre id=\"a9dc384d\" class=\"wp-block-code\"><code>SELECT * FROM employee ORDER BY salary;<\/code><\/pre>\n\n\n\n<pre id=\"13b422ff\" class=\"wp-block-code\"><code>SELECT * FROM employee ORDER BY salary ASC;<\/code><\/pre>\n\n\n\n<pre id=\"4f87df61\" class=\"wp-block-code\"><code>SELECT * FROM employee ORDER BY salary DESC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Hgba5\">\u6309\u591a\u5217\u6392\u5e8f<\/h3>\n\n\n\n<p id=\"uce5ab85f\">\u5148\u6309\u7167 <code>age<\/code> \u6392\u5e8f\uff0c\u5982\u679c <code>age<\/code> \u76f8\u540c\uff0c\u5219\u6309\u7167 <code>salary<\/code> \u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre id=\"9316bc05\" class=\"wp-block-code\"><code>SELECT * FROM employee\nORDER BY age, salary DESC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"u12Sd\">\u5c0f\u7ec3\u4e60<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u67e5\u8be2\u6240\u6709\u5458\u5de5\u4fe1\u606f\uff0c\u5148\u6309\u7167 <\/strong><code><strong>age<\/strong><\/code><strong> \u5347\u5e8f\u6392\u5e8f\uff0c\u5982\u679c <\/strong><code><strong>age<\/strong><\/code><strong> \u76f8\u540c\u5219\u6309\u7167 <\/strong><code><strong>hire_date<\/strong><\/code><strong> \u964d\u5e8f\u6392\u5e8f<\/strong><\/li>\n<\/ol>\n\n\n\n<pre id=\"24b3a04e\" class=\"wp-block-code\"><code>SELECT * FROM employee\nORDER BY age ASC, hire_date DESC;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>\u67e5\u8be2\u5404\u5c97\u4f4d\u5e73\u5747\u85aa\u8d44\u5927\u4e8e 10000 \u7684\u5c97\u4f4d\u540d\u3001\u5e73\u5747\u5de5\u8d44\uff0c\u7ed3\u679c\u6309\u5e73\u5747\u85aa\u8d44\u5347\u5e8f\u6392\u5217<\/strong><\/li>\n<\/ol>\n\n\n\n<pre id=\"b332d947\" class=\"wp-block-code\"><code>SELECT post, AVG(salary) AS avg_salary \nFROM employee \nGROUP BY post \nHAVING avg_salary &gt; 10000 \nORDER BY avg_salary ASC;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>\u67e5\u8be2\u5404\u5c97\u4f4d\u5e73\u5747\u85aa\u8d44\u5927\u4e8e 10000 \u7684\u5c97\u4f4d\u540d\u3001\u5e73\u5747\u5de5\u8d44\uff0c\u7ed3\u679c\u6309\u5e73\u5747\u85aa\u8d44\u964d\u5e8f\u6392\u5217<\/strong><\/li>\n<\/ol>\n\n\n\n<pre id=\"222150bc\" class=\"wp-block-code\"><code>SELECT post, AVG(salary) AS avg_salary \nFROM employee \nGROUP BY post \nHAVING avg_salary &gt; 10000 \nORDER BY avg_salary DESC;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bOSyS\">\u516b\u3001\u9650\u5236\u67e5\u8be2\u7684\u8bb0\u5f55\u6570\uff1a<code>LIMIT<\/code><\/h2>\n\n\n\n<p id=\"ued35ee9a\"><code>LIMIT<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u9650\u5236\u67e5\u8be2\u7ed3\u679c\u7684\u8bb0\u5f55\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"YcIEP\">\u793a\u4f8b<\/h4>\n\n\n\n<pre id=\"2ebc5aa2\" class=\"wp-block-code\"><code>SELECT * FROM employee ORDER BY salary DESC \nLIMIT 3;  -- \u9ed8\u8ba4\u521d\u59cb\u4f4d\u7f6e\u4e3a 0<\/code><\/pre>\n\n\n\n<pre id=\"5b80314d\" class=\"wp-block-code\"><code>SELECT * FROM employee ORDER BY salary DESC \nLIMIT 0, 5;  -- \u4ece\u7b2c 0 \u5f00\u59cb\uff0c\u5373\u5148\u67e5\u8be2\u51fa\u7b2c\u4e00\u6761\uff0c\u7136\u540e\u5305\u542b\u8fd9\u4e00\u6761\u5728\u5185\u5f80\u540e\u67e5 5 \u6761<\/code><\/pre>\n\n\n\n<pre id=\"cbd643c9\" class=\"wp-block-code\"><code>SELECT * FROM employee ORDER BY salary DESC \nLIMIT 5, 5;  -- \u4ece\u7b2c 5 \u5f00\u59cb\uff0c\u5373\u5148\u67e5\u8be2\u51fa\u7b2c 6 \u6761\uff0c\u7136\u540e\u5305\u542b\u8fd9\u4e00\u6761\u5728\u5185\u5f80\u540e\u67e5 5 \u6761<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"80db79ef-1\">\u5c0f\u7ec3\u4e60<\/h4>\n\n\n\n<p id=\"uf9e8e160\">\u5206\u9875\u663e\u793a\uff0c\u6bcf\u9875 5 \u6761\uff1a<\/p>\n\n\n\n<pre id=\"8e03e6e8\" class=\"wp-block-code\"><code>SELECT * FROM employee LIMIT 0, 5;<\/code><\/pre>\n\n\n\n<pre id=\"7b988b84\" class=\"wp-block-code\"><code>SELECT * FROM employee LIMIT 5, 5;<\/code><\/pre>\n\n\n\n<pre id=\"7a586450\" class=\"wp-block-code\"><code>SELECT * FROM employee LIMIT 10, 5;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"AaEZH\">\u4e5d\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u8be2<\/h2>\n\n\n\n<p id=\"ua3e88cd2\">\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"leMhD\">\u793a\u4f8b<\/h3>\n\n\n\n<pre id=\"d25bc377\" class=\"wp-block-code\"><code>SELECT * FROM employee WHERE name REGEXP '^ale';  -- \u540d\u5b57\u4ee5 'ale' \u5f00\u5934<\/code><\/pre>\n\n\n\n<pre id=\"9c0b43ee\" class=\"wp-block-code\"><code>SELECT * FROM employee WHERE name REGEXP 'on$';  -- \u540d\u5b57\u4ee5 'on' \u7ed3\u5c3e<\/code><\/pre>\n\n\n\n<pre id=\"df50e909\" class=\"wp-block-code\"><code>SELECT * FROM employee WHERE name REGEXP 'm{2}';  -- \u540d\u5b57\u5305\u542b\u4e24\u4e2a\u8fde\u7eed\u7684 'm'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"B4y7q\">\u5bf9\u5b57\u7b26\u4e32\u5339\u914d\u7684\u65b9\u5f0f\u5c0f\u7ed3\uff1a<\/h3>\n\n\n\n<pre id=\"578edc65\" class=\"wp-block-code\"><code>WHERE name = 'egon';  -- \u7cbe\u786e\u5339\u914d<\/code><\/pre>\n\n\n\n<pre id=\"7f8482ee\" class=\"wp-block-code\"><code>WHERE name LIKE 'yua%';  -- \u901a\u914d\u7b26\u5339\u914d<\/code><\/pre>\n\n\n\n<pre id=\"00599329\" class=\"wp-block-code\"><code>WHERE name REGEXP 'on$';  -- \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"XCth6\">\u5c0f\u7ec3\u4e60<\/h3>\n\n\n\n<p id=\"u24b2b99c\">\u67e5\u770b\u6240\u6709\u5458\u5de5\u4e2d\u540d\u5b57\u662f <code>jin<\/code> \u5f00\u5934\uff0c<code>n<\/code> \u6216\u8005 <code>g<\/code> \u7ed3\u5c3e\u7684\u5458\u5de5\u4fe1\u606f\uff1a<\/p>\n\n\n\n<pre id=\"4e204cc1\" class=\"wp-block-code\"><code>SELECT * FROM employee WHERE name REGEXP '^jin.*&#91;gn]$';<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5355\u8868\u67e5\u8be2\u7684\u8bed\u6cd5 \u4e8c\u3001\u5173\u952e\u5b57\u7684\u6267\u884c\u4f18\u5148\u7ea7\uff08\u91cd\u8981\uff09 \u6267\u884c\u987a\u5e8f\uff08\u4f18\u5148\u7ea7\uff09\uff1a \u7ec6\u89e3\u91ca \u5173\u952e\u5b57\u6267\u884c\u4f18\u5148\u7ea7\u7684\u5b8c\u6574\u793a\u4f8b  [&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-209","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\/209","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=209"}],"version-history":[{"count":1,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":210,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/210"}],"wp:attachment":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}